好吧,我正在製作這個遊戲,用戶可以在屏幕上拖動一個球,但它不應該離開遊戲區域。雖然我將推向了碰撞體,但它會反彈回來,如果我推得太緊,它就會脫離屏幕(我需要使它不會脫離屏幕,用戶可以自由拖動它當然,但在屏幕內)。當使用rigidbody2D拖動一個物體時,它會穿過碰撞體(牆壁)
有關如何解決此問題的任何提示?
這裏是我使用這對於拖動代碼:
using UnityEngine;
using System.Collections;
public class CircleManager : MonoBehaviour {
private bool dragging = false;
private Vector3 screenPoint;
private Vector3 offset;
// Pressionando
void OnMouseDown()
{
dragging = true;
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
// Arrastando
void OnMouseDrag()
{
Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
//i tried with both below.
//transform.position = cursorPosition;
transform.GetComponent<Rigidbody2D>().MovePosition(cursorPosition);
}
// Soltando
void OnMouseUp()
{
dragging = false;
}
}
謝謝!
*「當我將它推向碰撞體時,它會反彈回來」*您究竟期待它做什麼? – maksymiuk
我期待它不會穿過牆壁,對於缺乏清晰度感到抱歉!我需要它碰撞並停留在那裏,不允許玩家將它從屏幕上拖出。 – PossessWithin