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

moving objects around in-game

Discussion in 'Scripting' started by austen2fresh, Mar 25, 2011.

  1. austen2fresh

    austen2fresh

    Joined:
    Mar 11, 2011
    Posts:
    25
    Ok I have a script that lets me drag objects, this works fine. Inside that script, im trying to make it when i scroll up, the object moves further away (still able to be dragged) and when i scroll down it comes closer to the player. How to i change my script to when the mouse wheel is being scrolled, the objects is transformed up and down the z axis of the player (not the world). How would i do this? Here is my script:

    Code (csharp):
    1. while (Input.GetMouseButton(0))
    2.    {
    3.       var curScreenPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z);
    4.       var curPos = Camera.main.ScreenToWorldPoint(curScreenPos) + offset;
    5.      
    6.       held_obj = true;
    7.       Debug.Log("DOWN");
    8.       transform.position = curPos;
    9.        
    10.     if (Input.GetAxis("Mouse ScrollWheel"))
    11.         {
    12.        
    13.          transform.position = new Vector3(0, 0, 5 * Input.GetAxis("Mouse ScrollWheel"));
    14.        
    15.           Debug.Log("SCROLLED!");
    16.      }
    17.  yield;
    18.    }
    19. }
    When im dragging the block all it does is spin when I roll the mouse wheel. Also it spins on the worlds z axis and not the players. Please help.
     
  2. MegadethRocks

    MegadethRocks

    Joined:
    Dec 12, 2009
    Posts:
    162
    Here is a tiny example that only works with a static camera. I tried to keep the basic setup that you had in your script. To get this to work with a moving camera you would have to make a few changes, but this should get you started. In the example, I track the offset with a Vector3 called offset.

    Code (csharp):
    1. var held_obj : boolean;
    2. var screenPos : Vector3;
    3. var offset : Vector3;
    4.  
    5. function Start() {
    6.     held_obj = false;
    7.     offset = Vector3.zero;
    8.     screenPos = Vector3.zero;
    9. }
    10.  
    11. function Update () {
    12.     if (Input.GetMouseButtonDown(0))
    13.         MouseUpdate();
    14. }
    15.  
    16. function MouseUpdate()
    17. {
    18.     while (Input.GetMouseButton(0))
    19.    {
    20.         var curScreenPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
    21.         var curPos = Camera.main.ScreenToWorldPoint(curScreenPos);
    22.      
    23.         held_obj = true;
    24.         transform.position = curPos + offset;
    25.        
    26.         if (Input.GetAxis("Mouse ScrollWheel"))
    27.         {
    28.             var deltaZ = Input.GetAxis("Mouse ScrollWheel") * 5;
    29.             offset += Camera.main.transform.TransformDirection(Vector3.forward) * deltaZ;
    30.             transform.position = curPos + offset;
    31.         }
    32.         yield;
    33.     }
    34. }
    To get this code to work simply create an empty scene with the main camera at its default location and place a Cube with this script attached to it at (0,0,0).
     
  3. austen2fresh

    austen2fresh

    Joined:
    Mar 11, 2011
    Posts:
    25
    oh my god thank you so much it works! only problem is keeping it on the cameras forward position not the world. Can you explain what changes i would have to make for this to work?
     
  4. austen2fresh

    austen2fresh

    Joined:
    Mar 11, 2011
    Posts:
    25
    can someone explain how to make the object move in the cameras axis when i scroll and not the worlds?