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

Discussion My character moves only to the left without me doing anything

Discussion in 'Scripting' started by sprtolansgz, Dec 21, 2022.

  1. sprtolansgz

    sprtolansgz

    Joined:
    Dec 21, 2022
    Posts:
    1
    My character moves by itself without me doing anything, I have already reviewed the movement code several times, I even tried trying other methods with the same function (moving the character) and they all continued with the same problem (The code worked in yes, but my character moves infinitely by itself) I also installed and uninstalled versions of unity client to see if it could be solved by doing that but no

    Do you think it could be a problem with the unity client? I am using version 2022 although in version 2021 and 2019 the same thing happened to me, could it be some configuration that I touched by accident? And if so, how do I return it to its original state?

    Here is one of the codes I used, I also tried with CharacterController
    Link
    public class PlayerController : MonoBehaviour
    {
    private new Rigidbody rigidbody;

    public float speed = 10f;

    void Start()
    {
    rigidbody = GetComponent<Rigidbody>();
    }

    void Update()
    {
    float hor = Input.GetAxisRaw("Horizontal");
    float ver = Input.GetAxisRaw("Vertical");

    if (hor != 0.0f || ver != 0.0f)
    {
    Vector3 dir = transform.forward * ver + transform.right * hor;

    rigidbody.MovePosition(transform.position + dir * speed * Time.deltaTime);
    }


    }
    }

    And this is other script i tried using CharacterController

    public class PlayerController : MonoBehaviour {

    public float horizontalMove;
    public float verticalMove;
    public CharacterController player;

    public float playerspeed;

    void Start() {
    player = GetComponent<CharacterController>();

    }

    void Update() {

    horizontalMove = Input.GetAxis("Horizontal");
    verticalMove = Input.GetAxis("Vertical");


    }

    private void FixedUpdate()
    {
    player.Move(new Vector3(horizontalMove, 0, verticalMove) * playerspeed * Time.deltaTime);
    }

    }

    I used when I saw that they did not solve the problem, I saved and deleted from my project, right now I created a new file to focus on finding solutions to the problem, since with any script to move the character this happens to me with rigidbody or CharacterController I tried using the Debug.Log to find out if what was the problem but it didn't work either It sits on flat ground, I also turned gravity off but the problem was still working. No external forces are applied to the rigid body as it is a new project I made to focus on this problem. I don't have any other script currently in my project. This is the CharacterController script that I used and it didn't work for me, I'll give you the link where I got it too

    CharacterController:


    RigidBody:


    I would be very grateful if you help me solve this error that I have, I have been searching for a solution or explanation for two days in forums and youtube videosa
     
    Last edited: Dec 29, 2022
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    Have you used Debug.log/print to check that the horizontal and vertical inputs are both 0 when you aren't holding any buttons down?

    If something is making them non-zero, then you'll certainly get this unwanted movement! I'm not sure what would cause this, though...maybe an extra controller that's been left plugged in?
     
  3. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Ignore YouTube tutorials and start Unity training. I've wasted a lot of time looking for solutions from self-proclaimed Unity gurus, and everything is clear on the official site.

    Everything necessary is divided into units and topics, areas into short films that you tick off. The method of learning first see, understand and repeat works perfectly (completion time is about 12 Weeks, 4-5 hr a week).

    https://learn.unity.com/pathway/junior-programmer
     
    Chubzdoomer and chemicalcrux like this.
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    616
    Or the controller is failing.

    From the original post:
    Do you think it could be a problem with the unity client?

    Nope :) It would have already been reported and/or caught during testing. You have non-zero values which indicates to me that your controllers are failing and/or perhaps need cleaning.
     
  5. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Or he set the dead-zone to 0. No single controller is perfect.

    ProjectSettings -> InputManager -> Axes -> X -> Dead
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,612
    Yes, exactly. Use Debug.Log to see the value of Input.GetAxisRaw("Horizontal"). If you have a joystick (with an analog stick) plugged-in it will probably give you a non-zero number even if you aren't pressing the stick. Probably low like 0.001. You would just ignore any input unless it goes above a certain threshold.