简体   繁体   English

像鼠标位置的移动一样在 x 轴上移动对象 (Unity3d)

[英]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.当我单击屏幕上的任意位置时,我希望能够在 x 轴上移动我的对象,类似于我的鼠标位置。 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.您在脚本 #2 中的想法是正确的,但实现似乎有点偏离。 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.每一帧,当鼠标被按住(并且mousePoint != null )你减去鼠标的原始x位置和它的当前点之间的差异。 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.此外,您应该删除mouseDistance2的私有声明 - 您已经定义了两次。 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.此外,请确保您包含Time.deltaTime以便您的代码不依赖于帧率。 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最好从最后一个语句中删除 Time.deltatime 以便您可以让玩家顺利移动

 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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