[英]Unity C# How to avoid the tap and move by character only by swiping
我试图通过滑动/拖动来移动我的播放器,滑动效果很好,但是如果我点击屏幕的任何部分,它会将我的角色传送到 position。 我已经尝试过使用其他方法来移动它(如 transform.translate 或 rb.velocity),但感觉不够流畅。 所以我有点迷失为了解决它,这是我的代码:
if (Input.GetMouseButton(0))
{
Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
rb.MovePosition(curPosition);
}
下面是你可以用来拖动/滑动播放器而不被传送的代码; 将此脚本添加到您的播放器。 这适用于任何圆 object 否则,如果您的 object 包含任何其他对撞机,则将圆形对撞机替换为所需的对撞机
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragAndDrop : MonoBehaviour {
// touch offset allows ball not to shake when it starts moving
float deltaX, deltaY;
// reference to Rigidbody2D component
Rigidbody2D rb;
// ball movement not allowed if you touches not the ball at the first time
bool moveAllowed = false;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
// Add bouncy material tha ball
PhysicsMaterial2D mat = new PhysicsMaterial2D ();
mat.bounciness = 0.75f;
mat.friction = 0.4f;
GetComponent<CircleCollider2D> ().sharedMaterial = mat;
}
// Update is called once per frame
void Update () {
// Initiating touch event
// if touch event takes place
if (Input.touchCount > 0) {
// get touch position
Touch touch = Input.GetTouch(0);
// obtain touch position
Vector2 touchPos = Camera.main.ScreenToWorldPoint (touch.position);
// get touch to take a deal with
switch (touch.phase) {
// if you touches the screen
case TouchPhase.Began:
// if you touch the ball
if (GetComponent<Collider2D> () == Physics2D.OverlapPoint (touchPos)) {
// get the offset between position you touches
// and the center of the game object
deltaX = touchPos.x - transform.position.x;
deltaY = touchPos.y - transform.position.y;
// if touch begins within the ball collider
// then it is allowed to move
moveAllowed = true;
// restrict some rigidbody properties so it moves
// more smoothly and correctly
rb.freezeRotation = true;
rb.velocity = new Vector2 (0, 0);
rb.gravityScale = 0;
GetComponent<CircleCollider2D> ().sharedMaterial = null;
}
break;
// you move your finger
case TouchPhase.Moved:
// if you touches the ball and movement is allowed then move
if (GetComponent<CircleCollider2D> () == Physics2D.OverlapPoint (touchPos) && moveAllowed)
rb.MovePosition (new Vector2 (touchPos.x - deltaX, touchPos.y - deltaY));
break;
// you release your finger
case TouchPhase.Ended:
// restore initial parameters
// when touch is ended
moveAllowed = false;
rb.freezeRotation = false;
rb.gravityScale = 2;
PhysicsMaterial2D mat = new PhysicsMaterial2D ();
mat.bounciness = 0.75f;
mat.friction = 0.4f;
GetComponent<CircleCollider2D> ().sharedMaterial = mat;
break;
}
}
}
}
注:适用于 android
我认为您正在通过点击的 position 设置播放器的 position。 您可以根据滑动距离将其编辑为玩家的 position 变化:
Vector2 startPosition ;
if (Input.GetMouseButtonDown(0))
{
Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
startPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
}
if(Input.GetMouseButton(0))
{
Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
transform.position += (curPosition - startPosition);
startPosition = curPosition;
}
我不知道代码是否正常工作,但想法是这样。
您可以使用自由触摸或滑动 package。 首先,您需要从资产商店中选择一个免费工具,该工具可能会分别包含滑动和点击事件。我认为这会对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.