# How do I make an object face the mouse position, but only on one plane?

I want a game object to turn and look towards where the mouse was clicked (in world space). It works well when considering all 3 dimensions, but I really only care for the plane formed by the x and z axis, not the y.

I've tried to fix this by setting the y of the directionTarget to the y of the object's position, but that gives weird results.

My code looks like this:

Quaternion rotationTarget;
float rotationLerpProgress;
float rotationLerpDuration = 1f;

// Use this for initialization
void Start ()
{

}

// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit rayHit;
if (Physics.Raycast (ray, out rayHit)) {
var rayHitPoint = rayHit.point;
var rotationDirection = transform.position - rayHitPoint;
rotationDirection.Normalize ();
//rotationDirection.y = transform.position.y;
rotationTarget = Quaternion.LookRotation (rotationDirection);
rotationLerpProgress = Time.deltaTime;
}
//targetPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
}

if (rotationTarget != transform.rotation) {

transform.rotation = Quaternion.Lerp (transform.rotation, rotationTarget, rotationLerpProgress / rotationLerpDuration);
rotationLerpProgress += Time.deltaTime;

if (rotationLerpProgress >= rotationLerpDuration) {

transform.rotation = rotationTarget;
}

}

}


I must be missing something. How do I do this right?

• possible duplicate of Move player to mouse click – Roberto Jan 29 '14 at 3:43
• @Roberto: that question is entirely about a 2D-only world while this one is about mapping 3D rotation deltas to a plane. – Sean Middleditch Jan 29 '14 at 6:28

I'd project the rotationDirection into the horizontal plane before you normalize it, something like...

var rotationDirection = transform.position - rayHitPoint;


rotationDirection.y = 0;

rotationDirection.Normalize ();
rotationTarget = Quaternion.LookRotation (rotationDirection);


Note that the order of the subtraction used to define your rotationDirection there results in a vector from the rayHitPoint to transform.position, not the other way - is that as intended?

• Also, you don't actually need to normalize before calling Quaternion.LookRotation() - it handles working with non-unit vectors just fine (it may normalize a second time internally) – DMGregory Jan 29 '14 at 3:34
• Great! Ive learned something. I was using rotationDirection.y = transform.position.y; but instead i should have: rotationDirection.y = 0; Also the normalisation is not nescessary. I had done var rotationDirection = transform.position - rayHitPoint; but it should be the other way, as you pointed out. :) THANKS! – Daarwin Jan 29 '14 at 14:50
• @Lautaro I suggest you pick a good vector/matrices tutorial/book and study that for a while. Those skills are essensial for game development otherwise you will struggle if you want to make more complicated things. Sorry If I sound arrogant, that's only an advice and not meant to offense anyone. – concept3d Jan 29 '14 at 15:13
• @concept3d i agree. Im just trying to learn by doing small experiments. Im not doing a game just figuring out how to make stuff happen while i read tutorials. Care to recommend a good book for beginners? – Daarwin Jan 29 '14 at 19:24
• @Lautaro this one is good for beginners, it's not the best, but good enough to start with amazon.com/Primer-Graphics-Development-Wordware-Library/dp/… – concept3d Jan 29 '14 at 21:38