Search Unity

Mouse movement math

Discussion in 'Scripting' started by axesve, Jun 24, 2019.

  1. axesve

    axesve

    Joined:
    Feb 4, 2019
    Posts:
    7
    Hello!

    Been trying to figure out a somewhat easy task, but I am having a brainfreeze...

    I want to control an object by holding down left mouse button and moving the mouse, I want to move the object from its position towards the change from the (startMovement) and (Movement) variable, It works as intended, and the object moves just as I want.

    However when I release the mousebutton and try again the object resets to 0,0,0. Which is not what I want,


    Code (CSharp):
    1.  
    2. if (Input.GetMouseButtonDown(0)){
    3.    startMovement = Input.mousePosition;
    4. }
    5.  
    6. if (Input.GetMouseButton(0))
    7. {
    8.  
    9.  Movement.x = Input.mousePosition.x - startMovement.x;
    10.  Movement.z = Input.mousePosition.y - startMovement.y;
    11.  
    12.  cube.transform.position = new Vector3((Movement.x / 100), Movement.y, (Movement.z / 100));
    13. }
    14.  
    I am missing the cube position in the equation, but I have not found the correct way to do it.

    Hopefully, someone can point me in the right direction :)

    Note: I have gotten it to work by making it += at the last part, but that makes the cube feel laggy and continute to move even tho it has reached the position it was going to.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    On every MouseDown (the first Frame when you click) you are setting the position directly to the mouse position; you should add it as an offset to the current object position.
     
  3. axesve

    axesve

    Joined:
    Feb 4, 2019
    Posts:
    7
    I see, What would be the best way to achieve this?
     
  4. Cressidali

    Cressidali

    Joined:
    Feb 19, 2018
    Posts:
    13
    I don't see anything in the code that lets it stop, it will keep moving in the direction the mouse moved towards and moves faster depending on how far is the mouse from its original position. I don't know if it meant to behave that way or you wanted to stop once it reached the mouse position.

    Also if the code is in a fixedupdate it will cause it to lag.
     
  5. axesve

    axesve

    Joined:
    Feb 4, 2019
    Posts:
    7
    The only problem I have is that it resets back to 0,0,0. csofranz is correct about using it as an offset, I have not yet figured out where tho.
     
  6. Cressidali

    Cressidali

    Joined:
    Feb 19, 2018
    Posts:
    13
    Maybe something like this?
    Code (CSharp):
    1.  
    2. private void Update()
    3.     {
    4.         if (Input.GetMouseButtonDown(0))
    5.         {
    6.             startMovement = Input.mousePosition;
    7.             cubePos = cube.transform.position;
    8.         }
    9.  
    10.         if (Input.GetMouseButton(0))
    11.         {
    12.  
    13.             Movement.x = Input.mousePosition.x - startMovement.x;
    14.             Movement.z = Input.mousePosition.y - startMovement.y;
    15.  
    16.             cube.transform.position = new Vector3((Movement.x / 100), Movement.y, (Movement.z / 100)) + cubePos;
    17.         }
    18.     }
    19.  
    Add a vector3 with the cube's position at the start of the frame where you click. Then add that position when adding the movement.
     
  7. axesve

    axesve

    Joined:
    Feb 4, 2019
    Posts:
    7
    You are a lifesaver, thank you so much! Worked like a charm