Hi! im pretty new to making games in unity, and im kinda stuck.
Im making a 2D pixelart game where you fight as Tanks, using a controller. Im struggling with the rotation of the turret. (i have it so it will only aim in 8 directions)
As i have it now, it works. But i want to stop it from snapping over to the new controller angle. I want it to go around, as in over the other angles before its where its supposed to be. Like it would if it was a real one.
Is there an easy solution to make it rotate around like in the sketch i have?
The aiming script im using now is:
_rotXMove = Input.GetAxisRaw(HorizontalInput);
_rotYMove = Input.GetAxisRaw(VerticalInput);
_angle = Mathf.Atan2(_rotYMove, _rotXMove) * Mathf.Rad2Deg + 90f;
if (_rotXMove != 0 && _rotYMove != 0)
{
transform.localEulerAngles = new Vector3(0f, 0f, Mathf.Round(_angle / _SnapAngles) * _SnapAngles);
}
I really hope someone can help me.
Thanks!
So if you want a smooth rotation you can use either Quaternion.Slerp or Quaternion.RotateTowards –
private Quaternion targetRotation;
void Start () {
targetRotation = transform.localRotation;
}
void Update () {
//blah blah blah
if (_rotXMove != 0 && _rotYMove != 0) {
targetRotation = new Vector3 (0f, 0f, Mathf.Round (_angle / _SnapAngles) * _SnapAngles);
}
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, speed*Time.deltaTime);
//OR
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, targetRotation, speed*Time.deltaTime);
}
Where RotateTowards will move at a constant speed while Slerp will move faster at the beginning and slower as it approaches the target rotation.
**
If you want it to not rotate perfectly smooth but instead just snap to all the intermediate angles, there isn’t really a clean helper function that I can think of off the top of my head, but it should be relatively easy to calculate out the intermediate angles by hand and snap over timer sequentially