- Home /
LookAt function not working properly(cylinder looking at sphere)
I'm currently having difficulties letting the one end of the cylinder face a sphere.
I already created a script of:
//---------------------------------
var target: Transform;
function Update ()
{
transform.LookAt(target);
}
//-----------------------------
I've set the sphere as the target but the side/body of the cylinder is the one facing the sphere. How do I make the one end of the cylinder face the sphere instead of its side/body?
Help please
Answer by Bunny83 · Aug 02, 2012 at 09:28 AM
A common solution is to add your cylinder as a child to an empty gameobject. Set your cylinders position inside the empty GO to 0,0,0 and your rotation to your desired rotation, for example 90,0,0
Now attach your sctipt to the empty GameObject and it should work. The script still aligns the z-axis towards the target, but the z-axis of the empty GameObject. Since the cylinder is a child it will move and rotate along, but is pre-rotated by 90°.
Answer by whydoidoit · Aug 02, 2012 at 09:17 AM
You can do this:
transform.up = (target.position - transform.position);
But that can mess with the other rotations.
transform.LookAt(target);
transform.Rotate(Vector3.right, 90);
Answer by cupsster · Aug 02, 2012 at 09:18 AM
public Transform target;
void Update() {
transform.LookAt(target, Vector3.up);
}
Also try to replace Vector3.up with Vector3.right up or forward and you can also play with adding minus - sign in front of them if you want oposite variant of them.
That doesn't change the direction the object will be facing. The objects z-axis will always point at the target. The upvector just specifies the rotation around that target vector.
Also Vector3.up is a static "constant". The new keyword can only be used with an objects constructor. so either remove the "new", or use something like this:
new Vector3(0,1,0)
which is the same as:
Vector3.up
Your answer
Follow this Question
Related Questions
Connecting two spheres with a cylinder 1 Answer
how add cylinder with rigid body inside a sphere with rigid body? 0 Answers
How to make a cylinder follow two sphere 1 Answer
Move Spheres In a Hollow Cylinder 1 Answer
Problems with an object rotating a sphere surface (Quaternions and LookAt) 0 Answers