Top-Down 2D:
How can I rotate my player to look at the direction my Joystick is pointing to?
Here is the code that I’m using for my Joystick.
It moves my tank properly but at the same rotation (as expected, since I have not coded it yet to rotate/look at the position I’m moving towards to)
Vector3 tempJoyDelta;
public static Vector3 joyDelta;
Vector3 touchPosition;
public Transform centerPos;
Ray ray;
int currTouch = 64;
int sprintMask;
bool moving;
float saucerSpeed;
public GameObject tank;
void Awake () {
speed = 1.0f;
sprintMask = 1 << 8;
}
void Update () {
if(Input.touchCount > 0)
{
for(int i = 0; i < Input.touchCount; i++)
{
currTouch = i;
ray = Camera.main.ScreenPointToRay(Input.touches*.position);*
-
if(Physics.Raycast(ray, Mathf.Infinity, sprintMask))*
-
{*
-
if(Input.GetTouch(currTouch).phase == TouchPhase.Began && !moving)*
-
{*
-
moving = true;*
-
}*
-
if(Input.GetTouch(i).phase == TouchPhase.Ended || Input.GetTouch(i).phase == TouchPhase.Canceled)*
-
{*
-
moving = false;*
-
}*
-
if(Input.GetTouch(i).phase == TouchPhase.Moved || Input.GetTouch(i).phase == TouchPhase.Stationary)*
-
{*
-
touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, 0));*
-
}*
-
}*
-
}*
-
}*
-
if(moving)*
-
{*
-
joyDelta = centerPos.position - touchPosition;*
-
joyDelta.z = 0;*
_ tank.transform.Translate(((Vector3.forward * joyDelta.y) * speed) * Time.deltaTime);_
_ tank.transform.Translate(((Vector3.right * joyDelta.x) * speed) * Time.deltaTime);_
-
}*
}
Thanks!