[英]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.