Make a gameobject variable look at another gameobject variable

Hello,

I’m stumped on something that I think should be really easy. But for whatever reason I can’t figure it out. In my AI Script I need the AI to have the option to aim their barrel at target’s lower then they are. I was trying to use this line of code here.

 public GameObject Barrel;
public GameObject Target;

Barrel.transform LookAt (Target);

And I’ve tried this

Barrel.transform.LookAt(new Vector3(Target.transform.position.x, Target.transform.position.y, Target.transform.position.z));

For whatever reason it’s creating errors. Does anyone know the proper way to create this line of code? Sorry I’m a bit of an idiot when it comes to transforms and rotations.

What errors are you getting specifically?

1 Like

When in doubt, always check the documentation!

From the docs you can see .LookAt() is a method on transform, and it accepts either a Transform or a Vector3, so you write it like so:

Barrel.transform.LookAt (Target.transform);

or

Barrel.transform.LookAt (Target.transform.position);
1 Like

LookAt requires a transform for the first parameter, so instead of

Barrel.transform LookAt (Target);

you want

Barrel.transform LookAt (Target.transform);
1 Like

Thanks for the replies everyone. I totally forgot to use the position in there. Thanks a million :smile: