2d unity,如何仅在 x 轴上使用 touch/finger 拖动对象
2d unity , How to drag an object using touch/finger on x axis only
这里有人可以帮助我。我是团结的新手。我正在努力使对象仅在 x 轴/y 轴上移动,当对象触摸并向左拖动时,对象向左移动并在触摸或删除对象时停止。
using UnityEngine;
using System.Collections;
public class MoveScriptVertical : MonoBehaviour
{
//touch offset allows ball not to shake when it start moving
float deltax,deltay;
Rigidbody2D rb;
bool moveAllowed = false;
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
}
void Update ()
{
//touch event
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch (0);
//knowing the touch position
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
//touch phase
switch (touch.phase)
{
case TouchPhase.Began:
//if you touch the car
if (GetComponent<Collider2D> () == Physics2D.OverlapPoint (touchPos))
{
//get the offset between position you touch
deltax = touchPos.x - transform.position.x;
deltay = touchPos.y - transform.position.y;
//if touch began within the car collider
//then it is allowed to move
moveAllowed = true;
//restriction some rigidbody properties
rb.freezeRotation = true;
rb.velocity = new Vector2 (0, 0);
GetComponent<BoxCollider2D> ().sharedMaterial = null;
}
break;
//you move your finger
case TouchPhase.Moved:
//if you touches the car and move is allowed
if (GetComponent<Collider2D> () == Physics2D.OverlapPoint (touchPos) && moveAllowed)
rb.MovePosition (new Vector2 (0, touchPos.y - deltay));
break;
//you released your finger
case TouchPhase.Ended:
//restore intial parameters
//when touch is ended
moveAllowed = false;
rb.freezeRotation = true;
rb.gravityScale = 0;
PhysicsMaterial2D mat = new PhysicsMaterial2D ();
GetComponent<BoxCollider2D> ().sharedMaterial = mat;
break;
}
}
}
}
有一些库允许您拥有复杂的手指事件,例如 Touchscript。如果您选择此解决方案,您将在线找到教程。
最基本的方法是使用 unity 的输入 class 和一个不错的文档:https://docs.unity3d.com/ScriptReference/Input.html
这样您就可以知道手指何时触摸了屏幕或在屏幕上移动了。它会给你 x 和 y 坐标,所以如果你只想沿着 x 移动,你将只使用 x 值,例如使用
翻译你的对象
transform.Translate(new Vector3(x, 0f, 0f));
编辑
由于您使用的是 touchscript,请查看 TransformGesture,它应该允许您处理许多转换事件。如果你想有更多的控制,你可以改用 PressGesture、PanGesture 和 ReleaseGesture
这里有人可以帮助我。我是团结的新手。我正在努力使对象仅在 x 轴/y 轴上移动,当对象触摸并向左拖动时,对象向左移动并在触摸或删除对象时停止。
using UnityEngine;
using System.Collections;
public class MoveScriptVertical : MonoBehaviour
{
//touch offset allows ball not to shake when it start moving
float deltax,deltay;
Rigidbody2D rb;
bool moveAllowed = false;
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
}
void Update ()
{
//touch event
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch (0);
//knowing the touch position
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
//touch phase
switch (touch.phase)
{
case TouchPhase.Began:
//if you touch the car
if (GetComponent<Collider2D> () == Physics2D.OverlapPoint (touchPos))
{
//get the offset between position you touch
deltax = touchPos.x - transform.position.x;
deltay = touchPos.y - transform.position.y;
//if touch began within the car collider
//then it is allowed to move
moveAllowed = true;
//restriction some rigidbody properties
rb.freezeRotation = true;
rb.velocity = new Vector2 (0, 0);
GetComponent<BoxCollider2D> ().sharedMaterial = null;
}
break;
//you move your finger
case TouchPhase.Moved:
//if you touches the car and move is allowed
if (GetComponent<Collider2D> () == Physics2D.OverlapPoint (touchPos) && moveAllowed)
rb.MovePosition (new Vector2 (0, touchPos.y - deltay));
break;
//you released your finger
case TouchPhase.Ended:
//restore intial parameters
//when touch is ended
moveAllowed = false;
rb.freezeRotation = true;
rb.gravityScale = 0;
PhysicsMaterial2D mat = new PhysicsMaterial2D ();
GetComponent<BoxCollider2D> ().sharedMaterial = mat;
break;
}
}
}
}
有一些库允许您拥有复杂的手指事件,例如 Touchscript。如果您选择此解决方案,您将在线找到教程。
最基本的方法是使用 unity 的输入 class 和一个不错的文档:https://docs.unity3d.com/ScriptReference/Input.html
这样您就可以知道手指何时触摸了屏幕或在屏幕上移动了。它会给你 x 和 y 坐标,所以如果你只想沿着 x 移动,你将只使用 x 值,例如使用
翻译你的对象transform.Translate(new Vector3(x, 0f, 0f));
编辑
由于您使用的是 touchscript,请查看 TransformGesture,它应该允许您处理许多转换事件。如果你想有更多的控制,你可以改用 PressGesture、PanGesture 和 ReleaseGesture