Rotating To Look At Target

This lecture covered a pretty simple objective of rotating towards the target but used some heavier concepts. Did you have any questions regarding what we covered?

The Quaternion.Slerp() does not seem to work in my case.

I have tried using transform.LookAt(target) to replace all the codes inside FaceTarget() and it works fine. I’m not sure on what is wrong with the codes

Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.y));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * turnSpeed);

@Rick_Davidson
after reading the doc regarding Slerp it looks like that the third parameter is not time, but a range of [0,1] in which case we need this updated in every frame update. Perhaps this is new in Unity 2019?

here is the fix that I attempted, looks messy but it works for me

[SerializeField] float turnSpeed = 5f;
float progress = 0f;
float endTime = 0f;
float startTime = 0f;
Vector3 currentTargetPosition;

private void FaceTarget()
    {
        // target has moved, reset progress
        if (currentTargetPosition != target.position)
        {
            currentTargetPosition = target.position;
            progress = 0f;
            startTime = Time.time;
            endTime = Time.time + turnSpeed;
        }

        progress = (Time.time - startTime) / turnSpeed;
        if (endTime == startTime)
        {
            progress = 1f;
        }
        // get the normal vector towards the target
        Vector3 direction = (target.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.y));
        // spherical linear interpolation
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, progress);
     
    }
2 Likes

Having the same issue here. No matter what you set the turn speed to, the enemies are not rotating at all.

Also you appear to be using direction.y in the direction.z parameter, in your Quarternion.LookRotation function.

EDIT: Interestingly using the Zombie asset, has fixed the problem. So guessing there was some sort of issue with the way I had set up the primitives shrugg

I found some similar issues, and got things working in Unity 2019.3.4f1 with the following.

  1. I turned on Apply Root Motion in my enemy’s Animator. With this turned off, I was seeing the enemy refuse to rotate during the Move animation (which was an empty animation). With this on, the enemy started rotating during Move without any FaceTarget behavior of my own, but the Attack state still needs fixing.

  2. I used Quaternion.RotateTowards in FaceTarget (and called FaceTarget only during Attack, not in Move). It seems cleaner to me than trying to use Slerp.

    private void FaceTarget() {
        Vector3 direction = Vector3.Normalize(target.transform.position - transform.position);
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
        transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, Time.deltaTime * turnSpeed);
    }

Note that both the Nav Mesh Agent's Angular Speed and the speed that is required in RotateTowards are in degrees per second. A value of 5 is extremely slow, something more like 120 or 180 or something is reasonable.

7 Likes

@aseyfarth I confirm, “Apply Root Motion” works. Thanks!

Thanks asyfarth. This solved a problem I was having where the enemy wasn’t moving towards me since applying animations.

This does seem to override some of my animation though. My animation included movement on the y axis, so appeared that the enemy was bouncing towards you. Now it just does a squashing animation without the vertical movement.

Privacy & Terms