简体   繁体   中英

Move an object on x axis like the movement of the mouse position (Unity3d)

I want to be able to move my object on x axis similar to my mouse Position when I click anywhere on the screen. So that I can drag the object, but without jumping to the click Position just to follow the movement.

So I have made two scripts. In the first script the problem is that the object jumps directly to the Position I clicked. The second script works fine, but after clicking, the object always starts at on the same position from which I can drag it.

//1st script
public Vector3 screenPoint;
private float? mousePoint;
private float mousePoint2;
private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        mousePoint = Input.mousePosition.x;
        mousePoint2 = Input.mousePosition.x;
        screenPoint = Camera.main.WorldToScreenPoint(transform.position);
    }
    else if (Input.GetMouseButtonUp(0))
        mousePoint = null;

    if (mousePoint != null)
    {
        Vector3 curScreenPoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
        transform.position = new Vector3(curScreenPoint.x, transform.position.y, transform.position.z);
    }
}

}

//2nd script
private float difference;
private float? mousePoint;
private float mouseDistance1, mouseDistance2;
private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        mousePoint = Input.mousePosition.x;
        mouseDistance1 = Input.mousePosition.x;
    }
    else if (Input.GetMouseButtonUp(0))
        mousePoint = null;
    if (mousePoint != null)
    {
        float mouseDistance2 = Input.mousePosition.x;
        difference = mouseDistance2 - mouseDistance1;
        transform.position = new Vector3((transform.position.x + difference) / 188, transform.position.y, transform.position.z);
    }
}

}

Your idea in Script #2 is correct, but the implementation seems a tiny bit off. Every frame, when the mouse is held down (and mousePoint != null ) you subtract the difference between the mouse's original x position and its current point. Then, you add that difference to the object's position. That means that, even if the mouse is held in one spot after moving, the object is going to keep moving because the mouse delta hasn't changed.


To fix this, you should reset the mouse delta every frame instead of allowing the change in position to "compound." Also, you should remove the private declaration of mouseDistance2 - you've defined it twice. Thus, your code should look like this (with unused variables omitted):

private float? lastMousePoint = null;
private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        lastMousePoint = Input.mousePosition.x;
    }
    else if (Input.GetMouseButtonUp(0)) {
        lastMousePoint = null;
    }
    if (lastMousePoint != null)
    {
        float difference = Input.mousePosition.x - lastMousePoint.Value;
        transform.position = new Vector3(transform.position.x + (difference / 188) * Time.deltaTime, transform.position.y, transform.position.z);
        lastMousePoint = Input.mousePosition.x;
    }
}

By storing the last mouse position, you can calculate the distance that the mouse has moved between the last frame and the current one, and then add that to the object's position. Also, make sure you include Time.deltaTime so your code is not framerate dependent. I hope that helps - please let me know if you have any other questions.

it is better that you remove Time.deltatime from the last statement so you can allow the player to move smoothly

 private float? lastMousePoint = null;
private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        lastMousePoint = Input.mousePosition.x;
    }
    else if (Input.GetMouseButtonUp(0))
    {
        lastMousePoint = null;
    }
    if (lastMousePoint != null)
    {
        float difference = Input.mousePosition.x - lastMousePoint.Value;
        transform.position = new Vector3(transform.position.x + (difference / 188) , transform.position.y, transform.position.z);
        lastMousePoint = Input.mousePosition.x;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM