transform.LookAt is acting oddly.

Hello! I’m trying to make one object chase another, but that’s not where my problem lies; for some reason when I use transform.LookAt it makes the chaser move away from its target. (It does also look at the target.)

Here’s my code:

var target: Transform;
var chaseSpeed: float = 5;

function Update(){
transform.LookAt(target);
}

Code is correct.

How are you using the coordinate system? What is your up-vector.

transform.LookAt(target, Vector3.up);
transform.LookAt(target, -Vector3.up);

Perhaps you are not using Y as the upvector?

I’m not really sure. Anything that I don’t know about is probably at it’s default. I have added two placeholder cubes (one chaser, one player.) on a mesh surface. The code shown is the chaser’s only code. I don’t remember changing any Y values, or putting any parameters in LookAt.

How are you moving the object?

I’m not. It’s just for some reason it does. The only script acting on it is that.

Aha! I worked it out, I think. Basically; because the chaser is bigger it looks down at me slightly, but because of this it pushes itself into the ground which conseqentially pushes it away from me. Thus, can anyone tell me how to limit it’s rotation to one axis?

Okay I’ve got it now. Here’s my final script for simply making a block chase another if anyone wants it.

var target: Transform;
var targetcoords : Vector3;
var finaltarget: Vector3;

function Update(){
    targetcoords = transform.position;
    targetcoords.x = 0;
    finaltarget = Vector3(target.position.x, transform.position.y, target.position.z);
    transform.LookAt(finaltarget);
    transform.Translate(Vector3.forward * Time.deltaTime * 3);
}
1 Like