5
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ possible duplicate of Move player to mouse click \$\endgroup\$
    – Roberto
    Jan 29, 2014 at 3:43
  • 2
    \$\begingroup\$ @Roberto: that question is entirely about a 2D-only world while this one is about mapping 3D rotation deltas to a plane. \$\endgroup\$ Jan 29, 2014 at 6:28

1 Answer 1

3
\$\begingroup\$

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?

\$\endgroup\$
5
  • \$\begingroup\$ 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) \$\endgroup\$
    – DMGregory
    Jan 29, 2014 at 3:34
  • 1
    \$\begingroup\$ 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! \$\endgroup\$
    – Daarwin
    Jan 29, 2014 at 14:50
  • \$\begingroup\$ @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. \$\endgroup\$
    – concept3d
    Jan 29, 2014 at 15:13
  • \$\begingroup\$ @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? \$\endgroup\$
    – Daarwin
    Jan 29, 2014 at 19:24
  • \$\begingroup\$ @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/… \$\endgroup\$
    – concept3d
    Jan 29, 2014 at 21:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .