Rotate towards object script not working as intended

I’ll do my best to explain this. The usage is a bit unconventional and I’ve had a hard time getting the purpose across to others. I’m trying to get an object to aim at the player character using a spotlight with a slight offset. This object is a visual representation of the Main Camera and should not affect its behavior or movement.

Imagine the Lakitu camera guy from Mario Kart if that helps, but with a spot light attached.

With that explanation in mind, this is a script I thought fit pretty well in principle, but it doesn’t aim at the player like it’s supposed to. Independently maybe it would work just fine, but I’d like to figure out what’s wrong here.

public class LightTarget : MonoBehaviour
{
    public Transform target;
    public float speed = 5;

    private void update()
    {
        Vector3 direction = target.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(direction);
        transform.rotation = Quaternion.Lerp(transform.rotation, rotation, speed * Time.deltaTime);
    }
}

Here’s a screenshot of how I’ve got things setup.


I’ve got an Empty Object (named Main Camera), Systems (the camera and other various components), and then the Drone (a simple sphere) with the Light Target script attached to it. Child to the Drone is the standard Spot Light component and a basic Cylinder to determine if the Sphere and Spot Light are rotating or not.

Any help or suggestions would be greatly appreciated.

Hi @GIitch

Instead of calculating direction, you can just (maybe?) use Transform’s lookat. Set the look at target as your player/enemy/whatever position + some offset.

Hey,

try

// [...]
Quaternion toRotation = Quaternion.FromToRotation(transform.forward, direction); // instead of LookRotation( )
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.deltaTime);

Also try to put movements in FixedUpdate.

1 Like

Hmm. Well, it rotates with that script. But it doesn’t seem to be aiming directly at the player. Modifying the rotation doesn’t help, it just snaps back to the default rotation transform in Play mode.

That works perfectly! Thank you so much, Gurkenmensch!

I was running Quaternion.Lerp in LateUpdate and I was wondering why it was not working. Thank you.