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.

Bug Unable to move in Unity 2D

Discussion in 'Scripting' started by iamv3nxm, Mar 6, 2023.

  1. iamv3nxm

    iamv3nxm

    Joined:
    Feb 10, 2023
    Posts:
    5
    This is more of a 'noobie' question. But I have been making a game for the past day or two. I got the animation in, but now I'm unable to move. In Unity I'm using the Input Manager they provide, and I can technically move but I'm only able to look around, I am using animations in it so It just plays the idle animations and when I try to walk, it plays the walking animations but it does not move my character, it basically walks in place. It's incredibly annoying and I can use some help.

    I looked up a bunch of YouTube videos and none of them have helped. I even went to the Unity website to see if there's anything and so far nothing. I heard this is a good place to go to for help, so yeah. Here's the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.    public float speed;
    8.    private Rigidbody2D myRigidbody;
    9.    private Vector3 change;
    10.    private Animator animator;
    11.  
    12.    // Start is called before the first frame update
    13.    void Start() {
    14.        animator = GetComponent<Animator>();
    15.        myRigidbody = GetComponent<Rigidbody2D>();
    16.    }
    17.  
    18.    // Update is called once per frame
    19.    void Update() {
    20.        change = Vector3.zero;
    21.        change.x = Input.GetAxisRaw("Horizontal");
    22.        change.y = Input.GetAxisRaw("Vertical");
    23.        UpdateAnimationAndMove();
    24.    }
    25.  
    26.    void UpdateAnimationAndMove() {
    27.        if(change != Vector3.zero) {
    28.        {
    29.            MoveCharacter();
    30.        }
    31.            animator.SetFloat("MoveX", change.x);
    32.            animator.SetFloat("MoveY", change.y);
    33.            animator.SetBool("moving", true);
    34.        } else {
    35.            animator.SetBool("moving", false);
    36.        }
    37.    }
    38.  
    39.    void MoveCharacter() {
    40.        myRigidbody.MovePosition(
    41.            transform.position + change * speed * Time.deltaTime
    42.        );
    43.    }
    44. }
    I'm sure it's very simple but I know I am missing something. Again, help would be appreciated.
     
  2. ZingZangGames

    ZingZangGames

    Joined:
    Dec 11, 2020
    Posts:
    69
    Some points you should check:

    - is your player a static object? if so, make if dynamic by turning the checkbox at the top-right edge of the player object off
    - did the problem only occur after implementing animations? If so, go to your animator and make sure that "apply rootmotion" is turned off, you may also have to dive into your animations and fix some parameters if that is needed
    - as you're using a rigidbody controller, are there enough forces to move your character? Is any axis of the rigidbody locked or is he too heavy? What is the friction (it shouln't be too high, as that'll damp movement)? You should process movement-calculations in FixedUpdate()
    - are your physics-settings set up correctly?
    - could there be any other scripts that could block the movement of your character??

    If you're stuck, use Debug.Log() through your code so you know where the code is executed and where not! Hope this helps, these things are ususally the most annoying part of development and easily eat up hours of your life away :confused:

    Hope this helps :D