using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Drag : MonoBehaviour
{
float deltax, deltay;
float betax, betay;
private Quaternion Right = Quaternion.identity;
private Rigidbody2D rb;
//-----------------------------------------------------------------------------
public Vector3 moveVelocity;
public float movePower = 5f;
float moveVelocityx, moveVelocityy = 1;
GameObject gb;
//----------------------------------------------------------------------------
private void Start()
{
rb = GetComponent();
}
private void Update()
{
if (Input.touchCount > 0)// initiating touch event, if touch event takes place
{
Touch touch = Input.GetTouch(0); // get touch to take a deal with
Vector3 touchpos = Camera.main.ScreenToWorldPoint(touch.position); //obtain touch position
//transform.position = touch.position;
Debug.DrawLine(Vector3.zero, touchpos, Color.red);
switch (touch.phase)
{
case TouchPhase.Began:
deltax = touchpos.x - transform.position.x;
deltay = touchpos.y - transform.position.y;
break;
case TouchPhase.Moved:
rb.MovePosition(new Vector3(touchpos.x - deltax, touchpos.y - deltay,0));
break;
case TouchPhase.Ended:
rb.velocity = Vector3.zero;
break;
}
}
else
{
Movedd();
}
}
void Movedd()
{
moveVelocity = new Vector3(deltax, deltay,0);
transform.Translate(moveVelocity.normalized * movePower * Time.deltaTime);
}
}
============================================================================================
유저가 드래그를 하면 드래그한 방향으로 공이 움직이는 그런걸 만들고 싶음.
드래그하고 이동까지 성공했는데 그 이후에 이놈이 정상적인 방향으로 그러니까 유저가 드래그한 방향으로 이동하는걸 만들고 싶은데 어케 해야될지 모르겠음.
혹시 드래그를 놓은후에 움직이긴 하는데 방향이 이상한 거라면 deltax deltay를 TouchPhase.Began에서만 수정해서 그런 게 아닐까? Ended의 위치 - Began의 위치를 쓰면 잘 되지 어떨까? - dc App