Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Joystick problems and rotation

Discussion in 'Scripting' started by xxfelixlangerxx, Mar 12, 2019.

  1. xxfelixlangerxx

    xxfelixlangerxx

    Joined:
    Feb 6, 2019
    Posts:
    31
    Hey,

    i am programming my first mobile game but got kinda stuck. There is a problem with my playermovement and i dont know how to fix it. Here is where i need your help it might be just an obvious problem but i cant find it. The rotation joystick dosnt work and once the vertical input works fine but my horizontal inbut rotates my player 180 degress. It might just be really simple but i can figur it out.
    This is the code
    public class PlayerMovementScript : MonoBehaviour

    {

    Rigidbody rbplayer;

    public float moveforce = 5f;

    public Joystick playermovementjoystick;

    public Joystick playerrotationjoystick;

    private void Start()

    {

    rbplayer = this.GetComponent<Rigidbody>();

    }

    private void FixedUpdate()

    {

    Vector3 playerrotationtvector = new Vector3(playerrotationjoystick.Vertical * -1, playerrotationjoystick.Horizontal, 0);

    Vector3 playermovementvector = new Vector3(playermovementjoystick.Horizontal * moveforce, playermovementjoystick.Vertical * moveforce, 0);

    rbplayer.AddForce(playermovementvector);

    transform.LookAt(transform.position + playerrotationtvector);

    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    Check the first forum posts for guidelines about formatting code.

    Do you mean your horizontal input is reversed, i.e., left goes right, right goes left? If so, does it have anything to do with how you are multiplying by -1 when you create the playerrotationtvector variable?