Question by
Sohrab-Hamza · Jan 26, 2020 at 05:50 AM ·
vector3quaternionnavmeshmathf
How to make speed of rotation increase as distance between two objects decreases.
HI, I'm trying to make an enemy AI that engages the player only when they're in it's line of sight. But I also want it to rotate towards the player slowly when they're not in its LOS. This is my current implementation.
public float SensingDist = 50f;
public float rotationSpeed = 0.5f;
private void MoveTowardsPlayer()
{
float distfromPlayer = Vector3.Distance(player.transform.position, transform.position);
Vector3 targetDir = player.transform.position - transform.position;
float angle = Vector3.Angle(targetDir, transform.forward);
if (distfromPlayer <= SensingDist)
{
if(angle >=45)
{
Quaternion targetRotation = Quaternion.LookRotation(player.transform.position - transform.position);
float str = Mathf.Min(rotationSpeed * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
}
}
if (distfromPlayer <= SensingDist && angle < 45)
{
zombie.SetDestination(player.transform.position);
}
else
zombie.ResetPath();
}
But, I want the zombie's rotation speed to increase as the player gets closer to make it look more realistic. Also, any modifications to the method of rotation and calculation of distance between player and enemy is welcome.
Thanks!
Comment
Your answer
Follow this Question
Related Questions
InverseLerp for vector3 1 Answer
X-Axis rotation quirk 0 Answers
Having some problem with AI finding a vector3... 0 Answers
GameObject Smooth Movement 0 Answers