Search Unity

Question I would need with that Movement Script

Discussion in 'Getting Started' started by Tanips, Jul 4, 2022.

  1. Tanips

    Tanips

    Joined:
    Jul 3, 2022
    Posts:
    10
    Hei!
    Ive followed a tutorial yesterday on how to write a movement script but sadly unity isn't responding to it giving the error "Assets/character.cs(13,35): error CS0246: The type or namespace name 'RigidBody2D' could not be found (are you missing a using directive or an assembly reference?)" Could somebody more advanced take a look at my script?



    using UnityEngine;

    public class CharacterMovement : MonoBehaviour
    {

    public float MovementSpeed = 1;
    public float JumpForce = 1;

    private Rigidbody2D _rigidbody;

    private void Start()
    {
    _rigidbody = GetComponent<RigidBody2D>();
    }

    private void Update()
    {
    var movement = Input.GetAxis("Horizontal");
    transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;

    if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
    {
    _rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
    }
    }
    }
     
  2. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    Use CODE tags when you're displaying code.

    Also, the code syntax is VERY case sensitive and definitely watch your spelling when you're typing it in.
     
  3. Kreshi

    Kreshi

    Joined:
    Jan 12, 2015
    Posts:
    446
    You have no Rigidbody2D component attached to the GameObject where you have the CharacterMovement script attached. Just add Rigidbody2D to the GameObject and the error should be gone.
     
    Tanips likes this.
  4. Tanips

    Tanips

    Joined:
    Jul 3, 2022
    Posts:
    10
    fixed it! Thank you! :)