유니티에 익숙하지 않고 모바일 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;
}
}