So i’m basically making a small tank game and i want the turret of the tank to rotate with the mouse just like in World of Tanks but i can’t figure out how to do it.
I have tried myself but it failed completely so i took this code thinking it would work but indeed of doing what i want it to do, it just moves the turret inside the bottom of the tank clipping through it upside down.
public Camera camera;
public float speed;
private Vector3 mousePosition;
private Vector3 direction;
private float distanceFromObject;
void FixedUpdate()
{
mousePosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z - camera.transform.position.z));
GetComponent<Rigidbody>().transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2((mousePosition.y - transform.position.y), (mousePosition.x - transform.position.x)) * Mathf.Rad2Deg - 90);
distanceFromObject = (Input.mousePosition - camera.WorldToScreenPoint(transform.position)).magnitude;
GetComponent<Rigidbody>().AddForce(direction * speed * distanceFromObject * Time.deltaTime);
}
So does anyone know how i can make the turret rotate with the mouse position just like in World of tanks?