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);
}
}