Rigidbody2D.velocity Won't Move Object
So I have this fairly simple player controller. The animator works as intended but none of the Rigidbody lines seem to do anything.
public float Speed = 10f;
private Rigidbody2D Rigidbody;
private Animator Animator;
void Start()
{
Rigidbody = GetComponent<Rigidbody2D>();
Animator = GetComponent<Animator>();
}
void Update()
{
if (Input.GetAxis("Vertical") > 0f)
{
Animator.SetTrigger("WalkUp");
Rigidbody.velocity = new Vector2(0f, Speed);
}
if (Input.GetAxis("Vertical") < 0f)
{
Animator.SetTrigger("WalkDown");
Rigidbody.velocity = new Vector2(0f, -Speed);
}
if (Input.GetAxis("Vertical") == 0f)
{
Animator.SetTrigger("WalkForward");
Rigidbody.velocity = Vector2.zero;
}
}
I've tried using '0' instead of '0f', I've tried Input.GetAxisRaw. The animator's "apply root motion" is unticked. I'm sorry for showing you this much code but I feel it's necessary because I have no actual idea where the problem may be.
Answer by Tamariniak · Feb 12, 2017 at 08:37 AM
Yeah I did, I had 'simulated' unchecked though. It works now. Thanks for pointing me in the right direction
Answer by mr_noodler · Apr 25, 2020 at 03:06 AM
Rigidbody2D.velocity might not be able to move the object if there is an Animator that modifies the Transform. In this case the Animator should be in a GameObject that is a child of the Rigidbody2D.gameObject.
Answer by AMU4u · Feb 12, 2017 at 01:24 AM
Did you attach it to the GameObject with a rigidbody component added to it?
Your answer
Follow this Question
Related Questions
Player won't stop moving when key is released 1 Answer
Moving Character to mouse click with Rigidbody2D! 0 Answers
Unity2D Help with rigidbody character movement. 0 Answers
Rigid body vibrating? 0 Answers