- Home /
want to make a fixed rotation speed in Lerp no matter what the angle is
I'm working on a 2d Project and I'm New to rotations and Quaternoin,I want to make a fixed rotation speed in Lerp no matter what the angle is. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemyScript : MonoBehaviour
{
Rigidbody2D rb;
[SerializeField] GameObject Bullet, Player, FireSpot;
[SerializeField] float speed, Duration;
Vector3 Direction;
Quaternion Position;
float time = 0;
float Dot;
void Start()
{
rb = GetComponent<Rigidbody2D>();
Position = transform.rotation;
Dot = Mathf.Acos(Vector3.Dot(Vector3.right, Direction)) * Mathf.Rad2Deg;
}
void FixedUpdate()
{
transform.Translate(-Vector3.right * speed * Time.deltaTime);
if (time > 1)
{
Position = transform.rotation;
Dot = Mathf.Atan2(Direction.y, Direction.x) * Mathf.Rad2Deg;
time = 0;
print(Duration);
}
time += Time.deltaTime * Duration;
Direction = Player.transform.position - transform.position;
Debug.DrawRay(transform.position, Direction, Color.cyan);
Direction.Normalize();
transform.rotation = Quaternion.Lerp(Position, Quaternion.Euler(new Vector3(0, 0, Dot - 180)), time);
}
}
Answer by NivGames · Dec 11, 2021 at 08:38 AM
angleDeg = Vector2.Angle(-transform.right,Direction);
time = Time.DeltaTime / (Duration/angleDeg);
Answer by Captain_Pineapple · Nov 29, 2021 at 10:04 AM
hey there,
you basically just have to divide your Time.deltaTime * Duration
by the total angle you want to cover. So assuming that Dot
is your total angle your calculation each frame is Time.deltaTime * Duration/Dot
and it should work as you then have seconds * (angle/second) /angle
in your units. Thus leading to no unit which is what you want.
well just realized that Dot
is not the delta angle of your rotation. Just calculate that and divide by the total rotation delta instead of Dot
Your answer
