Hi, I’m building a 2D topdown shooter and I’m using two joysticks from the assets pack (one to move, and one to aim). I tried different approaches codewis but at the end at some point during gameplay the player starts rotating fast and slowly changing its position without any input from the joysticks. It seems it happens after some input from the move joystick and when I release the joystick the random rotation begins. Here’s the script attached to the player:
public class player : MonoBehaviour { public Joystick movejoystick; public Joystick aimjoystick; public float speed; Rigidbody2D rb;
- Vector2 move;
- Vector2 aim;
- // Start is called before the first frame update
- void Start()
- {
- rb = GetComponent();
- }
- // Update is called once per frame
- void Update()
- {
- //Movement v2 =============================================
- move.x = movejoystick.Horizontal;
- move.y = movejoystick.Vertical;
- //Rotation v2 ==================================================
- aim.x = aimjoystick.Horizontal;
- aim.y = aimjoystick.Vertical;
- float xAxis = aim.x;
- float yAxis = aim.y;
- float zAxis = Mathf.Atan2(xAxis, yAxis) * Mathf.Rad2Deg;
- if (xAxis != 0f || yAxis != 0f)
- {
- transform.eulerAngles = new Vector3(0f, 0f, -zAxis);
- }
- /* else
- {
- GetComponent().constraints = RigidbodyConstraints2D.FreezeRotation;
- }*/
- Debug.Log(movejoystick.Horizontal + " horizontal move joy");
- Debug.Log(movejoystick.Vertical + " vertical move joy");
- Debug.Log(aimjoystick.Horizontal + " horizontal aim joy");
- Debug.Log(aimjoystick.Vertical + " vertical aim joy");
- }
- private void FixedUpdate()
- {
- rb.MovePosition(rb.position + move * speed * Time.fixedDeltaTime);
- }
}