so I want to make a tank, the head of the tank should follow the rotation of the camera in a slow pace.
this is what i have
{
public Transform box; // the camera
private Vector3 boxrot; //the rotation of the camera
private Vector3 head; // rotation of the head
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
boxrot = new Vector3(box.rotation.eulerAngles.x, box.rotation.eulerAngles.y, box.rotation.eulerAngles.z);
head = this.transform.eulerAngles;
if (head.y > boxrot.y) { head.y -= 1f; };
if (head.y < boxrot.y) { head.y += 1f; };
this.transform.rotation = Quaternion.Euler(270,0,head.y);
}
}
it works fine but at the front of the tank the rotation degree’s switch from 0 to 360. so the head moves back becouse 0 < 360

the head(green) wil go the long way to the camera(blue)
Sxythe
2
You can use Quaternion.RotateTowards instead like this
transform.rotation = Quaternion.RotateTowards(transform.rotation, box.rotation, 1f);
If you only want to rotate it towards a certain axis, then you can set the eulerAngels of your camera to a Vector3, and use Quaternion.Euler() for the target like so
Vector3 rotation = box.rotation.eulerAngles;
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, rotation.y, 0), 1f);
thanks!! but is it possible to limit this to one axis only?