I’m trying to get my player to rotate 360 degrees in the direction of the mouse but that’s not being achieved. Any help would be much appreciated, thanks in advance.
Relevant piece of my code:
public class CharacterController : MonoBehaviour {
public float speed;
private float maxSpeed = 5.0f;
public float camSpeed;
private Rigidbody rb;
private Camera cam;
void Start () {
cam = GameObject.Find("Main Camera").GetComponent<Camera>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector3 mousePos = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20));
Vector3 direction = Vector3.Normalize(transform.position - mousePos);
float step = camSpeed * Time.deltaTime;
if (transform.forward != direction)
{
Vector3 newDir = Vector3.RotateTowards(transform.forward, direction, -step, 0.0f);
transform.rotation = Quaternion.LookRotation(new Vector3(newDir.x, 0, newDir.z));
}
}
}