im trying to make the mystical hands mod from blades and sorcery in my unity game but its not working. to set the position of the hands i have it go to the hands transform.forward and move further depending on the distance from the hand to the camera with an offset so the hand doesnt go inside you when the distance is too low. this works fine until i rotate and then the distance completely different, either way too close or way too far
code:
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.InputSystem;
public class MysticHands : MonoBehaviour
{
private LineRenderer Line;
[Header("Input")]
public InputActionProperty GrabAction;
[Header("References")]
public GameObject HandModel;
public Transform StartPos;
public Transform PlayerPos;
[Header("HandSettings")]
public LayerMask Grabable;
public float HandOffset = 5f;
public float HandMovementSpeed = 3f;
// Start is called before the first frame update
void Start()
{
HandModel.SetActive(false);
Line = gameObject.GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update()
{
float GrabValue = GrabAction.action.ReadValue<float>();
if (GrabValue > 0.5)
{
StartProject();
}
else
{
EndProject();
}
Line.SetPosition(0, transform.position);
Line.SetPosition(1, transform.forward * HandOffset * Vector3.Distance(transform.position, PlayerPos.position));
}
void StartProject()
{
HandModel.SetActive(true);
Vector3 Position = transform.forward * HandOffset * Vector3.Distance(transform.position, PlayerPos.position);
HandModel.GetComponent<Rigidbody>().velocity = Position - HandModel.transform.position * Vector3.Distance(Position, HandModel.transform.position) * HandMovementSpeed;
HandModel.transform.rotation = transform.rotation;
}
void EndProject()
{
HandModel.SetActive(false);
}
private void OnDrawGizmos()
{
Gizmos.DrawRay(transform.position, transform.forward * HandOffset * Vector3.Distance(transform.position, PlayerPos.position));
}
}