Make child object only rotate, look at path, and stay on parent


#1

I’m working on a 2D game. I have an “enemy” parent object with a line of sight range attached as a child.

I want to make the child object only rotate while the parent is following the path. I’ve tried attaching a separate AILerp component to the object and setting it’s transform to the parent in an update function, but all of those have either resulted in the child either not moving with the parent or at all, and annoyingly jittery movement of the child object, respectively.

Is there a way to make the transform.up of the child object to face along the path at all times?


#2

Hi

So you want the parent object not to rotate, but the child to rotate and face the path?

If so, a script like this would work:

void LateUpdate () {
    transform.rotation = Quaternion.LookDirection(Vector3.forward, GetComponentInParent<AILerp>().velocity);
}

You should not attach an AILerp script to the child as that will make it try to follow its own path.


#3

I have pasted the code provided into the NPCSight script, however I get errors stating that:

" ‘Quaternion’ does not contain a definition for ‘LookDirection’ "

" ‘AILerp’ does not contain a definition for ‘velocity’ and no extension method ‘velocity’ accepting a first argument of type ‘AILerp’ could be found (are you missing a using directive or an assembly reference?) "


#4

Right, I was just writing that blindly, I think it should be

void LateUpdate () {
    transform.rotation = Quaternion.LookRotation(Vector3.forward, GetComponentInParent<IAstarAI>().velocity);
}

(note that IAstarAI is an interface which AILerp implements)