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

[Solved] Object Jumps to New Position When Implementing Script

Discussion in 'Scripting' started by trlewi, Nov 27, 2017.

  1. trlewi

    trlewi

    Joined:
    Jan 3, 2017
    Posts:
    15
    So I am fairly new to C# and Unity and working on a little starter project. So I have this simple script that I put together to drag an object across the screen (on the x axis only) which I have gotten to work.

    But I do have this one issue I just can't figure out. I press run and the first click on the object causes the object to jump to a new position a bit lower on the x axis than it's original position. What am i doing wrong!?
    Code (CSharp):
    1. public class DragScript : MonoBehaviour {
    2.  
    3.  
    4.     public float distance = 10;
    5.     public float yPosition = 1f;
    6.  
    7.     void OnMouseDrag() {
    8.         Vector3 mousePosition = new Vector3 (Input.mousePosition.x, yPosition, distance);
    9.         Vector3 objPosition = Camera.main.ScreenToWorldPoint (mousePosition);
    10.  
    11.         transform.position = objPosition;
    12.     }
    13. }
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Try this:
    Code (CSharp):
    1. public class Drag : MonoBehaviour {
    2.  
    3.     float distance = 10;
    4.     float yPosition = 1f;
    5.  
    6.     void OnMouseDrag() {
    7.  
    8.         //Calculate any difference between the objects world position and the mouses Screen position converted to a world point
    9.         Vector3 offset = transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0));
    10.         //Keep track of the mouse position
    11.         Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance);
    12.         //Convert the screen mouse position to world point and adjust with offset
    13.         Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + offset;
    14.  
    15.         transform.position = new Vector3 (curPosition.x - offset.x, yPosition, distance);  //Update the position of the object in the world
    16.  
    17.     }
    18.  
    19. }
     
  3. trlewi

    trlewi

    Joined:
    Jan 3, 2017
    Posts:
    15
    Thank you QuinnWinters, unfortunatley the object still jumps on he z axis and as well as higher on the y-axis. My code had it jumping a couple points lower on the y-axis.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Here you go:)
    Code (csharp):
    1.  
    2. public float distance = 10;
    3.  
    4.     void OnMouseDrag()
    5.     {
    6.         Vector3 mousePosition = new Vector3(Input.mousePosition.x, 0, distance);
    7.         Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
    8.         objPosition.y = transform.position.y;
    9.         transform.position = objPosition;
    10.     }
    Previously, you had been setting the mouse position to a screen 'y' value of 1, which is the bottom of the screen.
     
    Last edited: Nov 28, 2017
  5. trlewi

    trlewi

    Joined:
    Jan 3, 2017
    Posts:
    15
    Hi @methos5k thank you for your response, I will try implementing your suggestion. It looks like you have set the value of y to 0, would this not make it jump to wherever "0" on the screen translates to. Or does the 0 mean no movement on the y-axis?

    Sorry still learning and just trying to get a better understanding.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The y of 0 in my response just means you're not concerned with the y value.
    But if you see a little later, I do assign the y value something.
     
  7. trlewi

    trlewi

    Joined:
    Jan 3, 2017
    Posts:
    15
    @methos5k Oh okay I think I see what you mean. You are not concerned with the y value of the mousePosition but are concerned with the y value of the object position which is set to equal the objects current set position.

    Also got a chance to implement and it worked like a charm, thank you!
     
    Last edited: Nov 29, 2017
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, np, you're welcome :)