简体   繁体   中英

How do I fix my 2D player movement in Unity?

I'm new to unity and have been trying to get the 2D Playermovement right for quite some time. Yet for some reason this error keeps popping up, anyone have any ideas?

this is the code I've been using:

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



public class PlayerMovement : MonoBehaviour
{
    
    private Rigidbody2d rb2D;

    private float MovementSpeed;
    private float JumpForce;
    private bool IsJumping;
    private float moveHorizontal;
    private float moveVertical;

    // Start is called before the first frame update
    void Start()
    {
        rb2D = gameObject.GetComponent<Rigidbody2d>();
       
        MovementSpeed = 3f;
        JumpForce = 60f;
        IsJumping = false;
    }

    // Update is called once per frame
    void Update()
    {
        moveHorizontal = Input.GetAxisRaw("Horizontal");
        moveVertical = Input.GetAxisRaw("Vertical");
    }


    void FixedUpdate ()
    {
        if(moveHorizontal > 0.1f || moveHorizontal < -0.1f)
        {
            rb2D.AddForce(new Vector2(moveHorizontal * MovementSpeed, 0f), ForceMode2D.Impulse);
        }
    }
}

在此处输入图像描述

Typo: It should be Rigidbody2D and not Rigidbody2d

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