- Home /
How to do transform.LookAt() from a different part of object?
So basically I want to use transform.LookAt() with my sprite but it faces it in a way that it's impossible to see from 2D view.
(don't ask about name of photo I couldn't be bothered to think of something to put)
I want it to face the target so you can see it. Heres my script btw
#pragma strict
var target : Transform;
var bulletSpeed : float = 10;
private var overallSpeed : float;
function Update () {
overallSpeed = bulletSpeed * Time.deltaTime;
transform.LookAt (target);
transform.position = Vector3.MoveTowards(transform.position, target.position, overallSpeed);
}
Thanks In Advance
Answer by HYPERSAVV · Dec 18, 2014 at 07:03 PM
Refer to the LookAt documentation. After pointing the forward vector at the desired position, it's rotating the object's up vector to the world up vector, which I'm assuming in your case is the issue. If the up vector on your object is facing the screen (z), it'll rotate it to the default which is (y) around the x-axis.
When you call LookAt, pass in a second parameter to define what the up vector is. It sounds like you are trying to create a 2D game within a 3D environment and you want your sprites to exist in the y-plane. If this is so, then you're up vector is +z, and not +y (which is the default f you leave the second param blank).
@isAndrewLong I'm kinda new to Unity but I'm not making it in a 3d environment. I just went into 3d to show the rotation of the object. Also because I'm quite new, not much of what you said made much sense to me
Hmm, let's try to break this down some more. What you are looking for is so that your object faces the other object, and what this means is that the forward vector of your object points in the direction of the other object. The Transform.forward documentation shows how you can find the angle necessary to rotate towards an object given your position and the position of the other object (see the second snippet). Once you have that angle, you can use Transform.Rotate to turn your object to face the second object.
I'm not sure how familiar you are with 3D $$anonymous$$ath, but it might be worth brushing up on it. Understanding 3D $$anonymous$$ath will help out a ton if you decide to stick with Unity. It's a bit much for this specific example, but this book is a great place to start.