Search Unity

2D Object to rotate to mouse.

Discussion in 'Scripting' started by TenTonApe, Feb 28, 2013.

  1. TenTonApe

    TenTonApe

    Joined:
    Feb 18, 2013
    Posts:
    18
    I'm trying to make a 2D shooter where the gun follows the mouse and fires in the direction you are aiming. I'm trying to get the gun to rotate to follow the mouse right now and I've tried

    Code (csharp):
    1.  
    2. Vector2 MS = Input.mousePosition;
    3. float Compare = Vector2.Angle(new Vector2(transform.position.x, transform.position.y), new Vector2(MS.x, MS.y));
    4. Vector3 Rot = new Vector3(0,0, Compare);
    5. transform.LookAt(Rot);
    6.  
    But the gun isn't rotating at all, what am I doing wrong?
     
    Last edited: Feb 28, 2013
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. TenTonApe

    TenTonApe

    Joined:
    Feb 18, 2013
    Posts:
    18
    Okay so I set my mouse position to world coords

    Code (csharp):
    1.  
    2. MS = Camera.mainCamera.ScreenToWorldPoint(Input.mousePosition);
    3.  
    And switched Compare to Vector3 Values

    Code (csharp):
    1. Compare = Vector3.Angle(transform.position, MS);
    But still the Gun does not rotate at all, I've set several of the values to public and watched them and they work, it seems like LookAt just isn't doing anything.
     
  4. TenTonApe

    TenTonApe

    Joined:
    Feb 18, 2013
    Posts:
    18
    I changed up the LookAt to:
    Code (csharp):
    1. transform.LookAt(new Vector3(MS.x, MS.y, 0));
    except now that it's rotating on the wrong axis (X) when I want it to rotate on the Z axis. I've tried switching around where MS.x and MS.y are but I can't get the cube to rotate right.
     
  5. Lyker

    Lyker

    Joined:
    Feb 27, 2013
    Posts:
    60
    Shouldn't it be like this?

    Code (csharp):
    1. transform.LookAt(new Vector3(MS.x, 0, MS.y));
     
  6. TenTonApe

    TenTonApe

    Joined:
    Feb 18, 2013
    Posts:
    18
    That makes it spin on the Y axis with some spin in the X.
    Quick second question how do I move the pivot? It's a cube I'm using with a material slapped on, I don't want it to rotate around the center.
     
  7. TenTonApe

    TenTonApe

    Joined:
    Feb 18, 2013
    Posts:
    18
    Okay so I rotated the cube 90 degrees so that an X-Axis rotation provides the effect I need, however I still have the pivot problem. Is there a way to offset the pivot of a basic cube in Unity?
     
  8. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    No exactly
    What you could do is put the cube as a child of an empty GO, with a different position, then apply the script to that instead
    create -> empty GO
    drag cube onto GO in the hierarchy
     
  9. TenTonApe

    TenTonApe

    Joined:
    Feb 18, 2013
    Posts:
    18
    Why is the Y rotation switching between 90 and 270? It works all the way up till it flips upside down. I even added a rigid body and restricted the Y rotation, but that did nothing.
     
  10. TenTonApe

    TenTonApe

    Joined:
    Feb 18, 2013
    Posts:
    18
    Bump. Any help?
     
  11. orangeflame

    orangeflame

    Joined:
    Nov 11, 2012
    Posts:
    3