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

FPS Controller Moving Me Backwards

Discussion in 'Scripting' started by Wolfyyy09, Apr 23, 2022.

  1. Wolfyyy09

    Wolfyyy09

    Joined:
    Apr 18, 2022
    Posts:
    2
    So I just finished this little movement script, and when I click play, I can move around. However, depending on the angle I am looking on the Y Axis, determines whether I'm being moved backwards without me clicking anything, or moving forward. any help? Here's the code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [RequireComponent(typeof(CharacterController))]
    5. [AddComponentMenu("Control Script/FPS Input")]
    6. public class FPSInput : MonoBehaviour
    7. {
    8.     public float speed = 6.0f;
    9.     public float gravity = -9.8f;
    10.  
    11.     private CharacterController charController;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         charController = GetComponent<CharacterController>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         float deltaX = Input.GetAxis("Horizontal") * speed;
    22.         float deltaZ = Input.GetAxis("Vertical") * speed;
    23.         Vector3 movement = new Vector3(deltaX, 0, deltaZ);
    24.  
    25.         movement.y = gravity;
    26.  
    27.         movement *= Time.deltaTime;
    28.         movement = transform.TransformDirection(movement);
    29.         charController.Move(movement);
    30.     }
    31. }
    32.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Print your raw inputs out; perhaps your input device or settings are giving you a small "backup" amount even when zeroed. This is called joystick drift or input drift and can be handled by deadbanding the input channel, eg, returning zero when input is below a desired minimum threshold.
     
  3. Wolfyyy09

    Wolfyyy09

    Joined:
    Apr 18, 2022
    Posts:
    2
    thank you for the answer, however, I am new to coding, and i would appreciate it if you could possibly explain that differently? :)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Eh, not really... it's just not a useful expenditure of anyone's time here.

    If you are not interested in learning how to debug an FPS controller you're writing, just go get one that works properly.

    There are MANY complete tutorials that work fine, or just use something like this:

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    If you DO feel like learning how to debug, start with this approach:

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494