158 lines
4.2 KiB
C#
158 lines
4.2 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using Mirror;
|
||
|
using UnityEngine.Animations.Rigging;
|
||
|
|
||
|
public class Interactions2 : NetworkBehaviour
|
||
|
{
|
||
|
[Header("Animations")]
|
||
|
[SyncVar]
|
||
|
private float takePropWeight = 0f;
|
||
|
public Rig takePropRig;
|
||
|
public Transform leftHandPos, rightHandPos;
|
||
|
public GameObject _eyesObject;
|
||
|
|
||
|
[Header("Prop Prefs")]
|
||
|
public GameObject propPrefab;
|
||
|
private RaycastHit _hit;
|
||
|
private Rigidbody _propRB;
|
||
|
|
||
|
[Header("Settings")]
|
||
|
public float maxHitDistance = 6f;
|
||
|
|
||
|
[Header("GUI Staff")]
|
||
|
public float hitDistance;
|
||
|
public bool canInterract;
|
||
|
|
||
|
[Header("Links")]
|
||
|
public StarterAssets.PersonController controller;
|
||
|
|
||
|
private PropGrab _grabbedProp;
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
takePropRig.weight = takePropWeight;
|
||
|
|
||
|
if (!isLocalPlayer) return;
|
||
|
|
||
|
if (Physics.Raycast(_eyesObject.transform.position, _eyesObject.transform.TransformDirection(Vector3.forward), out _hit, maxHitDistance, LayerMask.NameToLayer("Player")))
|
||
|
{
|
||
|
Debug.DrawRay(_eyesObject.transform.position, _eyesObject.transform.TransformDirection(Vector3.forward) * hitDistance, Color.yellow);
|
||
|
|
||
|
canInterract = _hit.collider.tag == "Prop" || _hit.collider.tag == "Button" ? true : false;
|
||
|
|
||
|
if (Input.GetKey(KeyCode.E))
|
||
|
{
|
||
|
if (_hit.collider.tag == "Prop" && _propRB == null)
|
||
|
{
|
||
|
_propRB = _hit.collider.gameObject.GetComponent<Rigidbody>();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
hitDistance = _hit.distance > 0 ? _hit.distance : maxHitDistance;
|
||
|
|
||
|
if (Input.GetKey(KeyCode.E))
|
||
|
{
|
||
|
if (_propRB != null)
|
||
|
{
|
||
|
Vector3 target = _eyesObject.transform.position + _eyesObject.transform.forward;
|
||
|
|
||
|
if (isClient)
|
||
|
{
|
||
|
CmdPropGrab(target, _propRB.gameObject, _eyesObject.transform.rotation);
|
||
|
SetPropRigWeight(1f);
|
||
|
|
||
|
CmdSetLayerMask(_propRB.gameObject, "Grab");
|
||
|
}
|
||
|
|
||
|
PropBehaviour propBehaviour = _propRB.GetComponent<PropBehaviour>();
|
||
|
setHands(leftHandPos, rightHandPos, propBehaviour);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (isClient)
|
||
|
{
|
||
|
if (_propRB != null)
|
||
|
{
|
||
|
CmdSetLayerMask(_propRB.gameObject, "Default");
|
||
|
}
|
||
|
|
||
|
SetPropRigWeight(0f);
|
||
|
}
|
||
|
_propRB = null;
|
||
|
}
|
||
|
|
||
|
if (Input.GetKeyDown(KeyCode.Q))
|
||
|
{
|
||
|
if (isClient)
|
||
|
{
|
||
|
CmdSpawnObject(_eyesObject.transform.position + _eyesObject.transform.forward, _eyesObject.transform.rotation);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#region Commands
|
||
|
[Command]
|
||
|
void CmdPropGrab(Vector3 target, GameObject prop, Quaternion dir)
|
||
|
{
|
||
|
RpcPropGrab(target, prop, dir);
|
||
|
}
|
||
|
|
||
|
[Command]
|
||
|
void CmdSetLayerMask(GameObject gameObject, string str)
|
||
|
{
|
||
|
RpcSetLayerMask(gameObject, str);
|
||
|
}
|
||
|
|
||
|
[Command]
|
||
|
void SetPropRigWeight(float value)
|
||
|
{
|
||
|
takePropWeight = value;
|
||
|
}
|
||
|
|
||
|
[Command]
|
||
|
void CmdSpawnObject(Vector3 pos, Quaternion rot)
|
||
|
{
|
||
|
GameObject gameObject = Instantiate(propPrefab, pos, rot);
|
||
|
NetworkServer.Spawn(gameObject);
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
#region Rpcs
|
||
|
[ClientRpc]
|
||
|
void RpcSetLayerMask(GameObject gameObject, string str)
|
||
|
{
|
||
|
gameObject.layer = LayerMask.NameToLayer(str);
|
||
|
}
|
||
|
|
||
|
[ClientRpc]
|
||
|
void RpcPropGrab(Vector3 target, GameObject prop, Quaternion dir)
|
||
|
{
|
||
|
Rigidbody propRB = prop.GetComponent<Rigidbody>();
|
||
|
|
||
|
Vector3 tarForce = target - propRB.transform.position;
|
||
|
Vector3 force = (tarForce * 20f - propRB.velocity) * Time.deltaTime * 30f;
|
||
|
|
||
|
propRB.AddForce(force, ForceMode.Impulse);
|
||
|
|
||
|
propRB.MoveRotation(dir);
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Methods
|
||
|
|
||
|
void setHands(Transform lh, Transform rh, PropBehaviour pb)
|
||
|
{
|
||
|
lh.position = pb.leftHandPos.position;
|
||
|
rh.position = pb.rightHandPos.position;
|
||
|
|
||
|
lh.rotation = pb.leftHandPos.rotation;
|
||
|
rh.rotation = pb.rightHandPos.rotation;
|
||
|
}
|
||
|
#endregion
|
||
|
}
|