Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Unity 2017.2 beta is now available for download.
    Dismiss Notice

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.  
    4.     v = theCamera.transform.position - mousePos;
    5.  
    6.     transform.LookAt(transform.position - v);
    7.  
    8.     // look = transform.position - mousePos;
    9.     //  transform.rotation = look;
    10.  
    11.     if (Input.GetButtonDown("Fire1")) {
    12.         ShootCannon();
    13.     }
    14. }
    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 strength = 5;
    2.  
    3. function Update () {
    4.  
    5.   zCam = -Camera.main.transform.position.z;
    6.   var mouseVector = Vector3(Input.mousePosition.x, Input.mousePosition.y, zCam);
    7.   var mPos = Camera.main.ScreenToWorldPoint(mouseVector);
    8.    
    9.   targetRotation.x = 0.0;
    10.   targetRotation.y = 0.0;
    11.    
    12.    
    13.   str = Mathf.Min (strength * Time.deltaTime, 1);
    14.   transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);  
    15. }
     
  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
  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
    tiagorpg likes this.