1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    Dismiss Notice

moving objects around in-game

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

  1. austen2fresh

    austen2fresh

    Joined:
    Mar 11, 2011
    Messages:
    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
    Messages:
    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.         MouseUpdate();
    13. }
    14.  
    15. function MouseUpdate()
    16. {
    17.     while (Input.GetMouseButton(0))
    18.    {
    19.         var curScreenPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
    20.         var curPos = Camera.main.ScreenToWorldPoint(curScreenPos);
    21.      
    22.         held_obj = true;
    23.         transform.position = curPos + offset;
    24.        
    25.         if (Input.GetAxis("Mouse ScrollWheel"))
    26.         {
    27.             var deltaZ = Input.GetAxis("Mouse ScrollWheel") * 5;
    28.             offset += Camera.main.transform.TransformDirection(Vector3.forward) * deltaZ;
    29.             transform.position = curPos + offset;
    30.         }
    31.         yield;
    32.     }
    33. }
    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
    Messages:
    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
    Messages:
    25
    can someone explain how to make the object move in the cameras axis when i scroll and not the worlds?