Problem with rotation to mouse position
Hi,
I got a 3D tank game where you should control a tank and the gun turret is pointing to your mouse position. This basically works but sometimes the rotation from the gun turret is "jumping" from 7 to 23, instead of pointing at the mouse position.
My script: (I found it on the internet)
using UnityEngine;
public class TankTurret : MonoBehaviour
{
Camera cam;
// Start is called before the first frame update
void Start()
{
cam = Camera.main;
}
// Update is called once per frame
void FixedUpdate()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector3 target = hit.point;
target.y = transform.localScale.y / 2f;
transform.LookAt(target);
}
}
}
I also got a Video where you can see what i mean by "jumping" from 7 to 23:
Thanks for reading :)
Comment
Your answer
Follow this Question
Related Questions
How to rotate Object with Mouse drag (specifications below) 2 Answers
Rotate Rigidbody on Y Axis based on Velocity on X and Z axis 3 Answers
Rotate towards mouse pointer 1 Answer
Rotate sphere to the direction of movement 0 Answers
Navmesh agent Auto Braking resets transform rotation after reaching destination 0 Answers