简体   繁体   English

Unity-通过鼠标/触摸拖动RigidBody2D游戏对象的最佳方法

[英]Unity - best way to drag a RigidBody2D Gameobject by mouse/touch

I am creating a 2D game in Unity and wanted to see if anyone has any suggestion on how I can improve the script below which I am using to drag a paddle in a breakout/arkanoid style game. 我正在Unity中创建一个2D游戏,想看看是否有人对我如何改进脚本的建议,在该脚本下方我会在突破/ arkanoid风格的游戏中拖动桨叶。 I know there are more complex ways to drag objects but this works ok for me but the only issue I encounter is that when I test my game on a mobile device the dragging is not 100% sharp and when I stop dragging the paddle seems to lag ever so slightly. 我知道有更复杂的拖动对象的方法,但是对我来说这行得通,但是我遇到的唯一问题是,当我在移动设备上测试游戏时,拖动并非100%清晰,而当我停止拖动桨时似乎滞后曾经如此轻微。 I don't have any issues with my mobile device as I have played other breakout games I downloaded from the Play store and the dragging is very crisp. 我玩过从Play商店下载的其他突破游戏时,移动设备没有任何问题,并且拖动非常清晰。

The script below is attached to the paddle. 以下脚本已附加到桨上。

Vector3 dist;
float posX;
float posY;

void OnMouseDown(){
    dist = Camera.main.WorldToScreenPoint(transform.position);
    posX = Input.mousePosition.x - dist.x;
    posY = Input.mousePosition.y - dist.y;

}

void OnMouseDrag(){
    Vector3 curPos = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - posY, dist.z);  

    Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);

    transform.position = worldPos;
}

I would advise doing your translation of the x and y-axis values using the mouse's position as it's being dragged via the subtracted difference between what the mouse's position value was as it's first pressed down. 我建议您使用鼠标的位置进行x和y轴值的平移,因为它是通过第一次按下时鼠标位置值之间的差值减去而被拖动的。

A summary of that would be: 总结如下:

  1. Store a 2D vector as a private field, called previousPos (so mousedown and mousemove can both access it) and set it's value to the result of the mouse's current position. 将2D向量存储为私有字段,称为previousPos(因此mousedown和mousemove都可以​​访问它),并将其值设置为鼠标当前位置的结果。
  2. In the mouse drag function, get the mouse position again and assign the value to a 2D vector called currentPos and subtract that from the previousPos, which gives you the x and y-axis values to set the paddle's transform position to. 在鼠标拖动功能中,再次获得鼠标位置,然后将该值分配给一个称为currentPos的2D向量,并从previousPos中减去该值,这将为您提供x轴和y轴值,以将球拍的变换位置设置为该值。
  3. Then update the previousPos at the end of the drag function, and assign the value of currentPos to it so that next time around it gives the new change and not the aggregated change. 然后在拖动功能的末尾更新previousPos,并为其分配currentPos的值,以便下次在它周围给出新更改,而不是汇总更改。

*You probably want to have a boolean that you set to true when mouse is down and at the end of the drag set it to false. *您可能想拥有一个布尔值,当鼠标按下时将其设置为true,在拖动结束时将其设置为false。 Use that bool in the drag as a check to see if you need to set the paddle position in the first place (only set paddle position if the boolean is true) -- Not sure if unity does this for you by only firing mouse dragged when the mouse is down or not. 在拖动中使用该布尔值作为检查对象,以检查是否需要首先设置桨叶位置(如果布尔值为true,则仅设置桨叶位置)-不确定是否仅在以下情况下触发鼠标拖动才能为您做到这一点鼠标是否按下。

An example in code: 代码示例:

private Vector2 _previousPos;
private bool _isMouseDown;

void OnMouseDown(){
    _isMouseDown = true; //because we're in the on mouse down function
    _previousPos = Input.mousePosition; //get the current mouse position
}

void OnMouseDrag(){
    if(!_isMouseDown)
        return;

    Vector2 currentPos = Input.mousePosition; //get updated mouse pos 
    Vector2 paddlePos = currentPos - previousPos; //the delta change

    transform.position = paddlePos; //new paddle position

    _isMouseDown = false; //drag is complete, mouse btn is no longer down
    _previousPos = currentPos; //reset previous pos for next mouse move
}

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

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