Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Joystick problems

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

  1. xxfelixlangerxx

    xxfelixlangerxx

    Joined:
    Feb 6, 2019
    Posts:
    31
    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 so i cant look downwards. My joystick works fine and outputs variables between 1 and -1.

    This is my code

    Code (CSharp):
    1. public class PlayerMovementScript : MonoBehaviour
    2.  
    3. {
    4.  
    5. Rigidbody rbplayer;
    6.  
    7. public float moveforce = 5f;
    8.  
    9. public Joystick playermovementjoystick;
    10.  
    11. public Joystick playerrotationjoystick;
    12.  
    13. private void Start()
    14.  
    15. {
    16.  
    17. rbplayer = this.GetComponent<Rigidbody>();
    18.  
    19. }
    20.  
    21. private void FixedUpdate()
    22.  
    23. {
    24.  
    25. Vector3 playerrotationtvector = new Vector3(playerrotationjoystick.Vertical * -1, playerrotationjoystick.Horizontal, 0);
    26.  
    27. Vector3 playermovementvector = new Vector3(playermovementjoystick.Horizontal * moveforce, playermovementjoystick.Vertical * moveforce, 0);
    28.  
    29. rbplayer.AddForce(playermovementvector);
    30.  
    31. transform.LookAt(transform.position + playerrotationtvector);
    32.  
    33. }
    Thanks for your help and have fun browsing
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    673
    Let me see if I can make sense of this. At Line 25, you are going to create a Vector3 that contains an x value somewhere from -1 to 1, and y value somewhere from -1 to 1. The z value is always 0. Then you are going to add that Vector3 to your player's position at Line 31, and have the transform look at that point. Now, you are using the single-parameter overload of LookAt, so "up" is going to be the positive y axis. Seems to me that this means the point you are looking at will be somewhere to your immediate right or immediate left, with the player looking up or down, in a range from 45 degrees above horizontal to 45 degrees below. If what you want is for your player to be looking all around in the xz plane, try replacing Line 25 with this:

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


    Let us know if that makes any difference.
     
    xxfelixlangerxx likes this.
  3. xxfelixlangerxx

    xxfelixlangerxx

    Joined:
    Feb 6, 2019
    Posts:
    31
    Sry you couldnt have known but my game lies within the y and x plane so it didnt fix it but thanks for considering.
     
  4. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    673
    Yeah, that would make my suggestion useless. We'll need to see more of your code to help, I think.
     
  5. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    673
    Oh, thinking about that, it occurs to me that LookAt assumes (unless you tell it otherwise) that "up" is the positive y direction. A game in the XY plane probably couldn't get LookAt to do anything useful. Maybe rewrite your game to play in the XZ plane?
     
  6. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    673
    This is a shot in the dark, but try this and see if it does any good:

    transform.LookAt(transform.position + playerrotationtvector, Vector3.forward);


    or

    transform.LookAt(transform.position + playerrotationtvector, Vector3.back);