Hello Guest

Author Topic: Rotate object on Z-axis to face current mouse position  (Read 6887 times)

guidez

  • Newbie
  • *
  • Posts: 7
    • View Profile
Rotate object on Z-axis to face current mouse position
« on: August 06, 2013, 04:09:49 am »
I've been at this for a few hours now, thinking it would be nice and simple, but guess not!

All that I want to do is rotate an object along it's Z-axis towards the mouse cursor.

I found some code (see the Rotation part of the full code below) that rotates the object as long as it stays close to the center. This object moves around though, and apparently the farther away the object moves from the center of the scene (0,0 x/y coords), the slower the rotation effect! I want the rotation to always stay constant (IE; instantaneous at this point), but can't for the life of me figure out how to translate the position of the mouse on the screen into the facing rotation Z-axis of the object!

Code: [Select]
using UnityEngine;
using System.Collections;

public class Object_Controller : MonoBehaviour {

    //Public Vars
    public Camera camera;
    public float speed;

    //Private Vars
    private Vector3 mousePosition;
    private Vector3 direction;
    private float distanceFromObject;

    void FixedUpdate() {

        if (Input.GetButton("Fire2")){

            //Grab the current mouse position on the screen
            mousePosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Input.mousePosition.z));

            //Rotates toward the mouse
            rigidbody.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mousePosition.y, mousePosition.x) * Mathf.Rad2Deg - 90);

            //Judge the distance from the object and the mouse
            distanceFromObject = (Input.mousePosition - camera.WorldToScreenPoint(transform.position)).magnitude;

            //Move towards the mouse
            direction = (mousePosition - transform.position).normalized;
            rigidbody.AddForce(direction * speed * distanceFromObject * Time.deltaTime);

        }//End Move Towards If Case

    }//End Fire3 If case
}
}

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: Rotate object on Z-axis to face current mouse position
« Reply #1 on: August 06, 2013, 10:45:04 am »
Hi Guidez,

I'm not sure exactly what you're trying to do, as I created a scene and used your code on some objects and the behaviour appeared to be what you wanted.  Bear in mind that the if you are holding down the mouse and moving it (i.e. so the rotation will be constantly updated on the objects), objects further away from the mouse will have less distance to rotate to face the mouse and therefore appear to rotate 'slower'.

I played around with the code and came up with a seemingly simpler version that appears to work with the limited testing I did, that doesn't use distances and adding force, but you get the same effect...

 void FixedUpdate()
   {
        if (Input.GetButton("Fire2"))
      {
            //Grab the current mouse position on the screen
            mousePosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Input.mousePosition.z));

   Vector3 relativePosition = mousePosition - gameObject.transform.position;;

            //Rotates toward the mouse
            rigidbody.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(relativePosition.y, relativePosition.x) * Mathf.Rad2Deg - 90);

       }//End Move Towards If Case

    }//End Fire3 If case


Maybe try and start with that and build up from there to get the results you want?

guidez

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Rotate object on Z-axis to face current mouse position
« Reply #2 on: August 06, 2013, 04:27:51 pm »
It looks like the issue was that I wasn't subtracting the mouse position from the gameObject's position, which led to that earlier interesting result. You are the third person to have corrected me, so thank you! Everything seems to be working now.

http://answers.unity3d.com/questions/508685/rotate-object-on-z-axis-to-face-current-mouse-posi.html