Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

move gameobject(hand) with mouse position

Discussion in 'Scripting' started by sriram90, Dec 2, 2010.

  1. sriram90

    sriram90

    Joined:
    Oct 14, 2010
    Posts:
    94
    hi guys,

    i want to move my player's hand object with mouse cursor movements...can any one help me...i have googled past some hours, still i'm struggling to find the answer...

    i want it as a C# script.

    thank you..
     
  2. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    When you say 'my player's hand object', do you mean the hand of a 3D character model? I.e something that's attached via an arm to a body? That's going to be difficult to do right, since you'll have to account for mouse movements that would make the hand go where it shouldn't be able to -- like into the character's torso, or too far away from the body for the length of the arm... In those cases, what should the rest of the character do? Walk sideways to accommodate the hand movement? Or should the hand stop moving?...

    If what you mean is you have a game object that looks like a hand, and that's the entire player representation -- i.e. what you want to do is move the whole player by moving the mouse -- it's easier to accomplish, but you'll still need to think carefully about what any given mouse movement should look like as a player movement.

    Post a more detailed explanation of what you're trying to achieve, along with any code you've managed to put together so far (even if it isn't working as you want; it'll help illustrate what you're trying to do).
     
  3. diabloroxx

    diabloroxx

    Joined:
    Jan 20, 2010
    Posts:
    71
    I agree to what Laurie has said. You need to explain what you mean by Moving a player's hand. Are you showing the hand of the player in the scene? Is it a 3rd person view/ fps view etc. So before you ask for a Script, please figure out what exactly you need and start developing it and post the script. Then am sure you will get better responses than simply asking for a script.
     
  4. sriram90

    sriram90

    Joined:
    Oct 14, 2010
    Posts:
    94
    ho oh,
    hi guys....

    i'll accept your points and my mistake.....the game object(player) is nothing but hand only...its not whole player...so i just want to move it....thats it...

    thank you guys for the reply ...
     
  5. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    In that case, you can use Input.GetAxis("Mouse X") and Input.GetAxis("Mouse Y") to find out how much the mouse has moved in 2D screen-space coordinates, convert those values into appropriate 3D world-space x/y/z-axis offsets, and apply them. I can't describe how to do the conversion from screen-space deltas to world-space deltas without knowing what sort of movement you want, though.

    I'd suggest looking at the keyboard-input based character controller scripts in the tutorials and trying to modify them to use mouse input instead (by just changing which Input.* calls the script uses). If you can't get the movement to work the way you want, you'll be able to post back with sample code of what you've got and a better description of how you would prefer it to work.
     
  6. sriram90

    sriram90

    Joined:
    Oct 14, 2010
    Posts:
    94

    hi laurie,


    Code (csharp):
    1. public float horiSpeed = 2.0F;
    2. public float verSpeed = 2.0F
    3.  
    4. void Update ()
    5.     {
    6.        Screen.lockCursor = true;
    7.           CursorMove();
    8.     }
    9.  
    10. void CursorMove()
    11. {
    12.      float h = horiSpeed * Input.GetAxis("Mouse X");      
    13.      float v = verSpeed * Input.GetAxis("Mouse Y");  
    14.  
    15.     transform.Translate(h,0,v);
    16.  
    17. }
    but its not moving well to my mouse position....its suitable only 2D not 3d....how to change it for 3d co ordinates???
     
  7. diabloroxx

    diabloroxx

    Joined:
    Jan 20, 2010
    Posts:
    71
    Why don't you try using the FPS Character controller and attach a Hand as the Child of the Camera so that it moves along with the Camera.
    Can you show some visual image of what you are trying to achieve. Alternatively, try looking into the FPS tutorial demo and try replacing the GUN with the Hand. Understand how the gun moves when you move the mouse and that should push you on the right tracks.
     
  8. sriram90

    sriram90

    Joined:
    Oct 14, 2010
    Posts:
    94
    hi diabloroxx,
    i'm able to understand you.. but i dont want move the camera...here the camera position should be stable....
     
  9. diabloroxx

    diabloroxx

    Joined:
    Jan 20, 2010
    Posts:
    71
    Then use Input.mousePosition and translate that to the transform.position of the game object.
     
  10. sriram90

    sriram90

    Joined:
    Oct 14, 2010
    Posts:
    94
    sorry to the late reply.....thats awesome....its working fine now...thank u diabloroxx....
     
  11. diabloroxx

    diabloroxx

    Joined:
    Jan 20, 2010
    Posts:
    71
    You are welcome. If you are planning to use physics, look for rigidbody.MovePosition as well.