简体   繁体   中英

Unity 2D: Rigidbody2D.velocity doesn't move character

I was making a 2D player controller and it worked until the day after I made a follow camera script, and now my character can't move but the animation still works. I tried using AddForce() instead of changing Rigidbody2D.velocity but that didn't work either, and after 2 or 3 days I still couldn't find a solution. Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;




public class PlayerController : MonoBehaviour
{
    Rigidbody2D rb2d;
    Animator animator;
    SpriteRenderer spriteRenderer;
    float direction;

    public float speed;

    // Start is called before the first frame update
    void Start()
    {
        direction = Input.GetAxisRaw("Horizontal");
        animator = GetComponent<Animator>();
        rb2d=GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    void Update()
    {
        transform.position = new Vector3(Mathf.Clamp(transform.position.x, -199, 197), 
        transform.position.y, transform.position.z);

        if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && ((Input.GetKey("right") || Input.GetKey("d")) || Input.GetKey("left") || Input.GetKey("a")))
        {
            speed = 20f;
            run();
        }
        else if (!(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && (Input.GetKey("right") || Input.GetKey("d")))
        {
            speed = 10f;
            walk();
        }
        else if (!(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && (Input.GetKey("left") || Input.GetKey("a")))
        {
            speed = 10f;
            walk();
        }
        else
        {
            speed = 0f;
            animator.Play("Player_idle");
        }
    }  

    void walk()
    {
        if (speed > 0) {
            animator.Play("Player_walk_right");
        }
        else if (speed < 0)
        {
            animator.Play("Player_walk_left");
        }
        //rb2d.AddForce(new Vector2(speed * direction * Time.deltaTime, 0));
        rb2d.velocity = new Vector2(speed * direction * Time.deltaTime, rb2d.velocity.y);
        Debug.Log(rb2d.velocity);
    }

    void run()
    { 
        if (speed > 0)
        {
            animator.Play("Player_run_right");
        }

        else if (speed < 0)
        {
            animator.Play("Player_run_left");
        }

        //rb2d.AddForce(new Vector2(speed * direction * Time.deltaTime, 0));
        rb2d.velocity = new Vector2(speed * direction * Time.deltaTime, rb2d.velocity.y);
        Debug.Log(rb2d.velocity);
    }
}

Here is an image of the inspector

If someone could help that would be appreciated.

If we look at your code, the problematic statement should be here:

rb2d.velocity = new Vector2(speed * direction * Time.deltaTime, rb2d.velocity.y);

One of these three variables must be zero to cause your problem.

Potential Problem 1: direction

You seem to only be setting direction once, in Start() :

direction = Input.GetAxisRaw("Horizontal");

You are probably not pressing any keys/moving any joysticks the frame that the scene starts up. That means direction ends up being 0 and is never changed again.

Either change your line to

rb2d.velocity = new Vector2(speed * Time.deltaTime, rb2d.velocity.y);

(excluding direction ) or move the direction assignment statement to your Update() method.

Potential Problem 2: Time.deltaTime

As said by @Daniel in the comments, the Time.deltaTime statement might be problematic.

  1. Check your project settings/other scripts to make sure Time.timeScale is not equal to zero.
  2. You could remove Time.deltaTime if #1 doesn't work, but I agree with you that the statement should be in there to keep the game consistent across framerates. Try changing Update() to FixedUpdate() ; if you do this you will need to use Time.fixedDeltaTime instead.

How to debug in the future

To debug this problem in the future, try inserting a breakpoint at the problematic line and attaching your IDE to Unity. If you hover over each of the variables, you should be able to see which variable is equal to zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM