Search Unity

problem with the joystick

Discussion in 'Scripting' started by bigdog16, Jun 11, 2019.

  1. bigdog16

    bigdog16

    Joined:
    Mar 5, 2018
    Posts:
    12
    Hello everybody I have a problem with my controller. If I push the joystick left, the object goes to the right,
    what am I doing wrong?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 200;
    9.  
    10.     public Joystick joystick;
    11.     private Rigidbody2D rb;
    12.     private Vector2 moveVelocity;
    13.  
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.  
    22.     private void Update()
    23.     {
    24.  
    25.         Vector2 moveInput = new Vector2(joystick.Horizontal, joystick.Vertical);
    26.         moveVelocity = moveInput.normalized * speed;
    27.  
    28.     }
    29.  
    30.  
    31.     void FixedUpdate()
    32.     {
    33.         rb.MovePosition(rb.position + moveVelocity * Time.deltaTime);
    34.     }
    35.  
    36.  
    37. }