Unity游戏用手指移动拖动对象
Unity game Drag Object with finger movement
我是Unity的新手,正在开发手机2d游戏,现在我可以在屏幕中心之前或之后触摸屏幕时让物体左右移动。但是我想触摸对象并将其拖动到 x 轴上,同时我的手指仍在触摸屏幕并移动,所以我希望对象与我的手指处于相同的 x 位置,
任何人都可以帮助我如何正确地做到这一点:
这是我在屏幕中心之前或之后触摸时如何移动对象的代码:
public class paddle : MonoBehaviour {
public Rigidbody2D rb;
public float speed;
public float maxX;
bool currentisAndroid=false;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
#if UNITY_ANDROID
currentisAndroid=true;
#else
currentisAndroid=false;
#endif
}
// Update is called once per frame
void Update () {
if (currentisAndroid == true) {
if (Input.GetTouch (0).position.x < Screen.width/2 && Input.GetTouch (0).phase == TouchPhase.Stationary)
moveLeft ();
else if (Input.GetTouch (0).position.x > Screen.width/2 && Input.GetTouch (0).phase == TouchPhase.Stationary)
moveRight ();
else
stop ();
} else {
float x = Input.GetAxis ("Horizontal");
//if (Input.GetTouch (0).position.x == rb.position.x && Input.GetTouch (0).phase == TouchPhase.Moved)
if (x == 0)
stop ();
if (x < 0)
moveLeft ();
if (x > 0)
moveRight ();
Vector2 pos = transform.position;
pos.x=Mathf.Clamp (pos.x,-maxX,maxX);
transform.position = pos;
}
}
void moveLeft()
{
rb.velocity = new Vector2 (-speed, 0);
}
void moveRight()
{
rb.velocity = new Vector2 (speed, 0);
}
void stop()
{
rb.velocity = new Vector2 (0, 0);
}
public float getposition()
{
return rb.position.y;
}
}
如果我理解正确的话:
1 - 使用相机从手指位置垂直投射到场景中。
2 - select 命中对象。
3 - 将你的相机映射到世界坐标并根据你的光线与地图或游戏的命中点移动该对象 object\objects。
- Physics.Raycast();
- RaycastHit.collider();
- Camera.main.ScreenToWorldPoint(Input.GetTouch(<0 or 1 or all>).position);
如果你想在地图上移动对象,你可以跟踪你的触摸,当它靠近角落时,在该方向上移动相机(水平 - 垂直)。
最简单的方法:
添加组件 DragRigidbody 脚本,您将能够通过鼠标或触摸屏触摸拖动对象。
我是Unity的新手,正在开发手机2d游戏,现在我可以在屏幕中心之前或之后触摸屏幕时让物体左右移动。但是我想触摸对象并将其拖动到 x 轴上,同时我的手指仍在触摸屏幕并移动,所以我希望对象与我的手指处于相同的 x 位置, 任何人都可以帮助我如何正确地做到这一点: 这是我在屏幕中心之前或之后触摸时如何移动对象的代码:
public class paddle : MonoBehaviour {
public Rigidbody2D rb;
public float speed;
public float maxX;
bool currentisAndroid=false;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
#if UNITY_ANDROID
currentisAndroid=true;
#else
currentisAndroid=false;
#endif
}
// Update is called once per frame
void Update () {
if (currentisAndroid == true) {
if (Input.GetTouch (0).position.x < Screen.width/2 && Input.GetTouch (0).phase == TouchPhase.Stationary)
moveLeft ();
else if (Input.GetTouch (0).position.x > Screen.width/2 && Input.GetTouch (0).phase == TouchPhase.Stationary)
moveRight ();
else
stop ();
} else {
float x = Input.GetAxis ("Horizontal");
//if (Input.GetTouch (0).position.x == rb.position.x && Input.GetTouch (0).phase == TouchPhase.Moved)
if (x == 0)
stop ();
if (x < 0)
moveLeft ();
if (x > 0)
moveRight ();
Vector2 pos = transform.position;
pos.x=Mathf.Clamp (pos.x,-maxX,maxX);
transform.position = pos;
}
}
void moveLeft()
{
rb.velocity = new Vector2 (-speed, 0);
}
void moveRight()
{
rb.velocity = new Vector2 (speed, 0);
}
void stop()
{
rb.velocity = new Vector2 (0, 0);
}
public float getposition()
{
return rb.position.y;
}
}
如果我理解正确的话:
1 - 使用相机从手指位置垂直投射到场景中。
2 - select 命中对象。
3 - 将你的相机映射到世界坐标并根据你的光线与地图或游戏的命中点移动该对象 object\objects。
- Physics.Raycast();
- RaycastHit.collider();
- Camera.main.ScreenToWorldPoint(Input.GetTouch(<0 or 1 or all>).position);
如果你想在地图上移动对象,你可以跟踪你的触摸,当它靠近角落时,在该方向上移动相机(水平 - 垂直)。
最简单的方法:
添加组件 DragRigidbody 脚本,您将能够通过鼠标或触摸屏触摸拖动对象。