I’m just starting to learn the Unity3D game development framework. I’m trying to make a cylinder “point” another object when some key is pressed.
public GameObject target;
void Update () {
if (Input.GetKeyDown(KeyCode.A)) {
???
}
}
I know that I have to use the target’s and the cylinder’s position to alter the cylinder’s rotation, but I can’t figure out how, I don’t think I understand what those Quaternions are yet.
I’d really appreciate any help!
Thanks,
Manuel
First, your cylinder needs some notion of ‘forward’ or its ‘pointing direction’ (my words) in the cylinder’s local space. For this you can assume (or visually see) either +X, +Y, +Z, -X, -Y, or -Z; or you can specify your own arbitrary vector pointing in some other direction.
Second, you need to a vector that points from your cylinder’s center to the other object’s center (you mentioned this already).
Now, you can use Unity’s
Quaternion.FromToRotation(...)to generate a quaternion that, if applied to your cylinder’s world rotation, will rotate your pointing direction to be in the direction of your other object. Done.Note that if your cylinder is more than a couple transforms deep, then you may need to alter the mechanics of this approach slightly to possibly account for parents’ transforms.