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

Question Movement of character(Ball) changed after adding Joystick

Discussion in 'Scripting' started by DerLuez, Jul 29, 2021.

  1. DerLuez

    DerLuez

    Joined:
    Jul 29, 2021
    Posts:
    3
    So i've been experimenting with moving a sphere and found a nice feeling script that made exactly what i wanted.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public Vector3 v3Force;
    6.  
    7.     public KeyCode keyPositive;
    8.     public KeyCode keyNegative;
    9.  
    10.     void FixedUpdate(){
    11.         if(Input.GetKey(keyPositive)){
    12.             GetComponent<Rigidbody>().velocity += v3Force;
    13.         }
    14.         if(Input.GetKey(keyNegative)){
    15.             GetComponent<Rigidbody>().velocity -= v3Force;
    16.         }
    17.  
    18.     }
    19.  
    20. }
    After that i wanted to test, how a joystick would feel in this situation and looked it up on youtube, as i dont have any experience with them and found following script:

    Code (CSharp):
    1. public class JSPlayerMovement : MonoBehaviour{
    2.    protected Joystick joystick;
    3.  
    4.     void Start(){
    5.         joystick = FindObjectOfType<Joystick>();
    6.  
    7.     }
    8.  
    9.     void Update(){
    10.         var rigidbody = GetComponent<Rigidbody>();
    11.  
    12.         rigidbody.velocity = new Vector3(joystick.Horizontal * 1f, rigidbody.velocity.y, joystick.Vertical * 1f);
    13.  
    14.     }
    15.  
    16. }
    17.  
    With that script i can move the Ball with the joystick, but the problem is, that the Ball doesnt move like it does in the first script. For some reason if I want to use the keyboard now, the balls is way slower than before. Another problem that i have, is that the ball instantly stops moving after letting go of the joystick or any of the movement keys.
    1. Does anyone know how to fix these issues and 2. how or can i put the scripts together into 1 script?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,949
    The issue is your first script only adds velocity, allowing the velocity to otherwise decay when not being added.

    The second script SETS the velocity to the input each frame, instantly changing it.

    The first one is also digital, while the second one is analog.

    Steps to success in bringing joystick input to the first one:

    1. collect all intention to temporary bool variables (do NOT act on it!)
    1a: keyboard
    1b: joystick
    1c: something else?

    2. act on those temporary boolean variables to do the movement as done in the first example
     
    DerLuez likes this.
  3. DerLuez

    DerLuez

    Joined:
    Jul 29, 2021
    Posts:
    3
    Thank you so much! Its now working perfectly fine as inteded.
     
    Kurt-Dekker likes this.