Hi all,
I have a slightly different problem with the joystick and movement than the similar topic I found. Namely. If I use my old script (without turning to face the joystick) my player moves perfectly through my scene (its a top-down game) but when I add the turning code my player turns perfectly but he moves all over the place and doesn’t follow the direction he looks at.
here is the code
using UnityEngine;
public class PlayerMovementHandler : MonoBehaviour
{
private float speed;
private Rigidbody2D controller;
public JoyStick moveJoyStick;
public PlayerMovementHandler()
{
speed = 3f;
}
// Use this for initialization
private void Start()
{
controller = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Vector3 dir = Vector3.zero;
Vector3 movement = new Vector3(dir.x, 0, dir.y);
dir.x = Input.GetAxis("Horizontal");
dir.y = Input.GetAxis("Vertical");
if(moveJoyStick.InputDirection != Vector3.zero)
{
dir = moveJoyStick.InputDirection;
}
transform.Translate(dir * speed * Time.deltaTime);
//the turn part of my code
float heading = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, heading - 90);
}
}