Search Unity

Rotating an object to face the mouse location?

Discussion in 'Scripting' started by croebot, Apr 18, 2009.

  1. croebot

    croebot

    Joined:
    Apr 18, 2009
    Posts:
    2
    Hey guys, what I'm trying to do, in theory is take an object that is restricted to 2d space (z is 0 always) and I want to make that object rotate to face the mouse pointer, I've done this in flash and in C using opengl. I think the confusion I'm having is getting the position of the cursor in world space. This is what I currently have, I've tried several trials and I'm finally giving in and asking for help :D

    Code (csharp):
    1. function Update ()
    2. {
    3.     mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
    4.  
    5.     v = theCamera.transform.position - mousePos;
    6.  
    7.     transform.LookAt(transform.position - v);
    8.  
    9.     // look = transform.position - mousePos;
    10.     //  transform.rotation = look;
    11.  
    12.     if (Input.GetButtonDown("Fire1")) {
    13.         ShootCannon();
    14.     }
    15. }
    Let me know if I'm even on the right track with this... thanks in advance!
     
  2. croebot

    croebot

    Joined:
    Apr 18, 2009
    Posts:
    2
    In case anybody else is trying to figure this out, here's what I finally came up with that works great. :wink:
    Code (csharp):
    1. var target : Transform;
    2. var strength = 5;
    3.  
    4. function Update () {
    5.  
    6.   zCam = -Camera.main.transform.position.z;
    7.   var mouseVector = Vector3(Input.mousePosition.x, Input.mousePosition.y, zCam);
    8.   var mPos = Camera.main.ScreenToWorldPoint(mouseVector);
    9.    
    10.   targetRotation = Quaternion.LookRotation (mPos - transform.position);
    11.   targetRotation.x = 0.0;
    12.   targetRotation.y = 0.0;
    13.    
    14.    
    15.   str = Mathf.Min (strength * Time.deltaTime, 1);
    16.   transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);  
    17. }
     
    nhuthuynh likes this.
  3. thecreatrix

    thecreatrix

    Joined:
    Dec 19, 2008
    Posts:
    94
    I'm having a similar problem. Camera.main.ScreenToWorldPoint() only ever returns the camera's world position no matter what vector I use as a parameter.

    I'm sure I've done something stupid and possibly illegal in more than one country. But I can't figure out why it ignores the vectors I give it to convert (ie, current Input.mousePosition).
     
  4. thecreatrix

    thecreatrix

    Joined:
    Dec 19, 2008
    Posts:
    94
    Just as I posted for more help, I realized that changing the Z to -Camera.main.transform.position.z will correct my problem. I have no idea why, but it works. :)

    Here's my code, which is a bit different from the above.

    Code (csharp):
    1.  
    2. Vector3 mouseScreenPosition = new Vector3( Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z );
    3.  
    4. mouseWorldPosition = Camera.main.ScreenToWorldPoint( mouseScreenPosition );
    5.  
    6. transform.LookAt( mouseWorldPosition, Vector3.up );
     
  5. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    In the vector you send to ScreenToWorldPoint are you always setting the z component to 0? That will just give you the camera transform every time.

    In world space, screen points correspond to rays going from the camera's position off into the distance. The z value you put in is basically the distance from the origin, and at 0 distance it will always just be the camera's position. To get a useable point you have to specify a distance (z value) at which to calculate the position.
     
  6. thecreatrix

    thecreatrix

    Joined:
    Dec 19, 2008
    Posts:
    94
    Ok, so because it's a ray (I thought it was just computing a point by converting the coordinates directly), without a distance (ie, Z = 0) it doesn't go anywhere. That makes sense. Thanks for the explanation.
     
  7. TheCrazedMadman

    TheCrazedMadman

    Joined:
    May 20, 2009
    Posts:
    49
    Wow, finding this code just made my day. Just one question, when I apply this code to my model...it rotates around first (facing diagonally backwards), is there a way to fix this (like an offest or something). I know it should be a simple fix, just dont understand the code well enough to do it myself (tried adding/subtracting values to the z coordinate, but no such luck).

    Im using the code from croebot, btw.

    Thanks
     
  8. gelatinouscactus

    gelatinouscactus

    Joined:
    Feb 18, 2013
    Posts:
    7
    I did this with the following few lines of code(c#) in the update function

    Code (csharp):
    1.  
    2. float h = Input.mousePosition.x - Screen.width / 2;
    3. float v = Input.mousePosition.y - Screen.height / 2;
    4. float angle = -Mathf.Atan2(v,h) * Mathf.Rad2Deg;
    5.        
    6. transform.rotation = Quaternion.Euler (270, angle, 0);
    7.  

    put the angle variable on whichever axis you would like it to rotate around and attach the script to the object you would like to rotate.
     
    SophiaLily, Waup and tiagorpg like this.