Question

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

Was it helpful?

Solution

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.

OTHER TIPS

Is there a reason as to why you are using Quaternions i would use Quaternions.Eular angles which represent Quaternions as a vector 3 which is how we commonly understand angles.

what Ducky said is correct however if you are having troubles with Quaternions i would recomend not using them until you have a better understanding of these angle sets.

hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top