"LookAt" in the Y axis?

hello, i want to make a model (with a Character Controller attached) to LookAt an Object, but i want to rotate only in their Y axis (to maintain the vertically).

how i can do it?

i tried this but i can´t get to run:

You do that by looking at them while ignoring all y data in the function call. Like this:

transform.LookAt(Vector3(otherObject.position.x, transform.position.y, otherObject.position.z));

12 Likes

uauhh amazing!!!

thanks you very much!!!

Hello, I’ve used the code but my object shakes like hell.

transform.LookAt(Vector3(target.position.x, transform.position.y, target.position.z));

If I use otherObject it doesn’t look at my moving playable unit.

And any idea how I can add that Quaternion Slerp to it to make it smoother?

Thanks,

If the object is jittering or shaking, try moving the call to LookAt() to LateUpdate() and see if that makes a difference.

when i try… it says position is not a member of vector 3…

can someone help? does this script work on unity 3?

actualy i want this script in a diferent way… i want to use look at, but only with the Z and X axis, without looking at the Y

Post your script in its entirety; also, post the error message, and indicate (e.g. via comment) what line generates the error.

Probably, but we don’t know what script you’re referring to.

It sounds to me like you’re looking for the same thing as the OP, but it’s not entirely clear one way or the other. I’d recommend getting your current script working first though, as you may find it does in fact do what you want.

okay, heres the part of the script that im having trouble (since the script itself is very big and redundant )

function OnTriggerEnter(other : Collider){

[COLOR="seagreen"]//(this right here is a statement to check the 
nearest object tagged with "aimable" once it finds it, the 
character will look at it )[/COLOR]

if(other.gameObject.tag == "aimable"  wasTriggered == false){
mySphere.radius = 0;
targets = other.gameObject;
wasTriggered = true;
		
animation.Stop("subweapon1");
animation.Play("subweapon2");
		
[COLOR="seagreen"]//(this "alvo" variable is the object the 
character has to look at.. but .. the problem is, 
when this object is waay above him.. he 
rotates its Y axis to look at it.. and it looks 
ridiculous... i wanted to make just the X or 
Z look at the target direction )[/COLOR]

var alvo = targets.transform.position;

[COLOR="seagreen"]//(now im using this transform.LookAt 
(alvo) because despite being ridiculous.. at least it works..)[/COLOR]

transform.LookAt (alvo) ;
		
var sub = Instantiate (misile,GameObject.Find("SUBWEAPONPOINT").transform.
position,Quaternion.identity);
		
sub.transform.LookAt (alvo) ;

[COLOR="seagreen"]//(i also have the "sub" projectile looking at the 
object, but in this case,its ok to turn its Y axis..)[/COLOR]

    }
}

okay, basicly… this is my script up there… the only diference was that i replaced that part with : transform.LookAt (alvo)

by this :

transform.LookAt(Vector3(alvo.position.x, transform.position.y, alvo.position.z));

that was de script that i found here… offcourse this would not fit to me because this script is specify to look at the Y axis… but i was just testing if it work then i would do the necessary changes

‘alvo’ is a Vector3, not a transform, so this:

transform.LookAt(Vector3(alvo.position.x, transform.position.y, alvo.position.z));

Is incorrect (since Vector3 does not have a field or property named ‘position’).

Try this instead:

transform.LookAt(Vector3(alvo.x, transform.position.y, alvo.z));

Now it works just fine!.. thanks mr.anders!

1 Like

Do you get the error: Vector3 can not be used like a method? when using:

transform.LookAt(Vector3(alvo.x, transform.position.y, alvo.z));

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

The new keyword means you’re creating a new instance of Vector3 with the stated arguments.

1 Like