Search Unity

Making an object snap to mouse

Discussion in 'Scripting' started by Krispy, Oct 14, 2005.

  1. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    How would I go about doing this? I bascally need to translate the pixel coordinates into a Vector3 position.

    I tried using Input.mousePosition directly, but it's all whacked up.

    Thanks
     
  2. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    :? Ok, my aim is to replicate the behaviour of a Breakout paddle.

    I haked something together, but there has to be a better way, right?

    (on Paddle)


    Code (csharp):
    1.  
    2. public Vector3 mousePosDiff;
    3. public CameraScript MainCamera;
    4.  
    5. void Update () {
    6.         mousePosDiff.x = MainCamera.mousePos.x / snapFactor;
    7.         mousePosDiff.y = initY;
    8.         mousePosDiff.z = initZ;
    9.         transform.position = mousePosDiff;
    10. }
    11.  
    (on Camera)
    Code (csharp):
    1.  
    2. public Vector3 mousePos;
    3.     void Update () {
    4.         mousePos = camera.ViewportToWorldPoint(new Vector3(Input.mousePosition.x - Screen.width/2, Input.mousePosition.y - Screen.height / 2, Input.mousePosition.z));
    5.     }
    6. }
    7.  
    I know, it's ugly.

    Help? Anyone?
    :(

    Thanks
     
  3. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Krispy, I am doing something similar (ie. Breakout like), but I am just using the keys. The following is working reasonable well:

    Code (csharp):
    1.  
    2. // Attached to the paddle
    3.  
    4. x = Input.GetAxis("Horizontal");
    5.  
    6. if (x > 0  cameraLook.WorldToViewportPoint(transform.position).x <= 1) {
    7. transform.position += Vector3(0.5, 0.0, 0.0) * x;
    8. }
    9.  
    10. if (x < 0  cameraLook.WorldToViewportPoint(transform.position).x >= 0) {
    11. transform.position += Vector3(0.5, 0.0, 0.0) * x;
    12. }
    13.  
    If you really want to use the mouse, what type of behavior are you going for? A snap to the mouse, a constant movement rate if the mouse is on one side of the paddle or the other, or something else? I might be able to help, I just am not sure of the exact behavior you are trying to acheive.

    Now, do you know how to do the bounce off the paddle? I am having trouble getting the bounce to work correctly.
     
  4. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    Aarg, I solved it. It's still a complete and utter hack.


    Code (csharp):
    1.  
    2. public Vector3 mousePosDiff;
    3. public Camera MainCamera;
    4. public Transform targetRange;
    Code (csharp):
    1.  
    2. mousePosDiff.x = (Input.mousePosition.x - Screen.width / 2) / (Screen.width / 2) * (Mathf.Abs(targetRange.position.z - transform.position.z) - (MainCamera.fieldOfView / 10));
    3.         transform.position = mousePosDiff;
    4.  
    OMG Hax0rz.
    :D

    As for paddle bouncing. Did you know you can set the velocity of a Rigidbody directly? so you could use:




    Code (csharp):
    1.  
    2. void OnCollisionEnter(Collision hit){
    3.          if (hit.rigidbody) {
    4.                 hit.rigidbody.velocity = new Vector3(hit.rigidbody.velocity.x, hit.rigidbody.velocity.y * -1, hit.rigidbody.velocity.z);
    5.  
    6.  
    7.             }
    8.     }
    nifty huh? I'm using a more realistic approach and just adding a force when it hits.
     
  5. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    <edit> This is now working (thanks to aarku) just using bounciness and resetting the velocity on each collision. If the velocity is not reset the ball slows down, even if bounciness is set to one and the drags are set to 0. Also, had to set the timestep down to .005 (again, thank you aarku).
    </edit>

    I have tried setting the velocity directly, but the angle is off on the bounce, even doing as you suggest. Perhaps something else is affecting the ball with how I have things set up. I am using colliders and currently have my paddle "is Kinematic" checked while the ball's "is Kinematic" is unchecked.

    How are you keeping the paddle from being affected by the ball?<edit>not an issue any more. "Is Kinematic" checked does the trick</edit>

    <edit> Still would like to know this one if anyone knows the answer</edit>
    On a related, but side note, how do you set a mass above 20? Everytime I try to set the mass above 20 in the Inspector, it is set back to 20. Do I just need to do it through the script?
     
  6. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    I'd still really like to know how to convert the mouse's position into a Vector3's x y with an arbitrary z position. I'm trying to make an object look at the mouse. Seems simple huh? It proably is but I cant figure it out, so any help is appreciated.
     
  7. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Don't know if this will work since I do not have Unity handy at this time, but how about something like:
    Code (csharp):
    1.  
    2. theObjectYouWantToLookAtTheMouse.transform.LookAt(Input.mousePosition);
    3.  
     
  8. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    No, Input.mousePosition is in pixel coordinates, which are X:0 to screen.width and Y: 0 to screen.height, plus Z but i'm not sure what that's for.
     
  9. socksy

    socksy

    Joined:
    May 21, 2005
    Posts:
    244
    That will be for when the OTEE guys get their hands on the Revolution controller SDK, pity they didn't include rotation, perhaps there is an undocumented Input.mouseRotation variable?
     
  10. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Ok, how about:
    Code (csharp):
    1.  
    2. theX = Input.mousePosition.x / camera.pixelWidth
    3. theY = Input.mousePosition.y / camera.pixelHeight
    4. theZ = ? // how about 0.0 or camera.transform.position.z
    5.  
    6. theObjectYouWantToLookAtTheMouse.transform.LookAt(Vector3(theX, theY, theZ);
    7.  
    You could might want to use ScreenToViewportPoint or ScreenToWorldPoint for the Input.mousePosition.
     
  11. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);

    ray.origin -> the mouse position in world space at the near plane of the camera
    ray.direction -> the direction of the mouse in world space
     
  12. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    I would think it behaves like a joystick (with rotors to controll x,z and y in space)? That would make the most sence and then maybe with a force controller to check how fast you are slashing/stabbing whatever (and the lenght of it).
    But I don't think Nintendo supports this kind of thing (unity I mean) + wouldn't it need a complete rewrite?

    Atleast it could work in (my) theory :D
     
  13. socksy

    socksy

    Joined:
    May 21, 2005
    Posts:
    244
    It's not a question whether or not Nintendo support it, more of Unity supporting it. There would presumably be an API for game devs to detect the position of the controller, with an SDK tagged along. Otherwise, it would be pointless. All the OTEE guys would have to do would be pay loads of money, prove that they know how to program games, and then adapt the current scripting API so that Input stuff can also detect Rev controller. Like they got HID to work :D

    Ofcourse, it isn't nearly that simple and that description was completely wrong. The end.

    Repeat after me: "I know nothing about thisbrand new scheme that Nintendo have to intregrate indy devs into the Revolution development."
     
  14. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Ok.. I know noth.. WTF? You nearly got me to say something here (hypnotist?).. Anyway Im not saying it's that easy, im saying thats theory.. It's not like the gyrois an all-changing new event in town (it's actually from 1852), so I would guess they are using existing technoligy and not reenventing the wheel..

    Besides, just for your information Socky: I don't know what Im talking about! :D

    EDIT: I was talking about the posibilities of using a theoretic Revolution controller with Unity, not porting Unity over to the Revolution.

    I don't think Nintendo would allow otee's Unity to compile products for the Revolution either, because they have to drag all of us along, which I would A) make the pricetag on Unity very big (and it would mean that Otee had to check every one of us to see if we had the required gameexperience), or B) mean that Unity was out of comercial production and only used by Otee. So unless you have some revolutionary news Im stopping my dreaming about this now.