0
\$\begingroup\$

I'm trying to make a 2D Unity platform game for a school project, but I can't seem to get the enemy to move.

using UnityEngine;

public class EntityMovement : MonoBehaviour {
    public float speed = 1f;
    public Vector2 direction = Vector2.left;
    
    private Rigidbody2D rigidbody;
    private Vector2 velocity;
    
    private void Awake() {
        rigidbody = GetComponent<Rigidbody2D>();
    }
    
    private void OnBecameVisible() {    
        enabled = true;
    }
    
    private void OnBecameInvisisble() {
        enabled = false;
        GetComponent<Rigidbody>().WakeUp();
    }

    private void OnEnable() {
    
    }

    private void OnDisable() {
        GetComponent<Rigidbody>() .velocity = Vector2.zero;
        GetComponent<Rigidbody>().Sleep();
    }
    
    private void FixedUpdate() {
        velocity.x = direction.x * speed;
        velocity.y += Physics2D.gravity.y * Time. fixedDeltaTime;
    
        rigidbody.MovePosition(rigidbody.position + velocity * Time.fixedDeltaTime);
    }
}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ For your future questions, it would be helpful to include all error messages as well that you get. In your code example you would get a MissingComponentException since you can't have both a RigidBody and RigidBody2D at the same time on your GameObject. The error message itself is telliing you already the why it is not working. \$\endgroup\$
    – Zibelas
    Dec 2, 2023 at 13:21

2 Answers 2

1
\$\begingroup\$

In the OnEnable() and OnDisable() functions, you used GetComponent<Rigidbody>() which is for 3D instead of GetComponent<Rigidbody2D>(). You could either switch it to GetComponent<Rigidbody2D>() (why would you do this), or logically replace it with the Rigidbody2D rigidbody.

\$\endgroup\$
3
  • \$\begingroup\$ What is the difference between 2D and 3D? other than an additional axis? \$\endgroup\$
    – agone
    Dec 9, 2023 at 0:04
  • \$\begingroup\$ The component Rigidbody is for 3D and the component Rigidbody2D is for 2D. If you assigned the component Rigidbody2D on a 2D object, you can't access a component called Rigidbody when you don't have that. In short, Rigidbody2D only works for 2D and Rigidbody only works for 3D. If you are trying to access Rigidbody, you are trying to access a component you don't have. \$\endgroup\$ Dec 16, 2023 at 12:26
  • \$\begingroup\$ Rigidbody2D provides an interface to the Box2D physics engine, while Rigidbody provides an interface to the PhysX physics engine. So while the Unity components the present on the surface look similar, they tie into completely separate parts of the code, managing two distinct physics universes. \$\endgroup\$
    – DMGregory
    Dec 16, 2023 at 16:40
0
\$\begingroup\$

You should fix the OnEnable() and OnDisable() method's GetComponent() part. This should be Rigidbody2D. Also you have already set rigidbody in Awake(). Therefore you do not need to use GetComponent anymore.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This seems to repeat the same advice already present in the answer from two weeks ago. What new information are you hoping to add? \$\endgroup\$
    – DMGregory
    Dec 16, 2023 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .