Rigidbody2D falls slowly with MovePosition?
I'm trying to make a simple 2D character controller, but with the following code the player falls extremely slowly! I've seen similar problems before, but I can't seem to find a working answer. I know I could directly set rb.velocity, but I'm curious as to why this is not working. Any thoughts? Thanks! using UnityEngine;
public class PlayerMotor : MonoBehaviour {
private Rigidbody2D rb;
private Vector2 vel;
private bool jumped;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
public void Move(float h)
{
vel = new Vector2(h, rb.velocity.y);
//transform.Translate(vel * Time.deltaTime);
}
public void Jump()
{
jumped = true;
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + vel * Time.fixedDeltaTime);
}
}
rb.position + vel * Time.fixedDeltaTime
You should use transform to set position. Use rigidbody to set position does nothing but break engine if you dont know what you are doing.
Ok. Just a few questions: 1.`transform.position = rb.position + vel * Time.fixedDeltaTime` goes into FixedUpdate() correct? 2. Why use rb.position and not transform.position? for example: transform.position = transform.position + vel * Time.fixedDeltaTime
' and put that into Update() over FixedUpdate()? I just wanna try to understand things and not copy it verbatim. Thanks again!
Anything related to Physics must be in fixedUpdate(). You was doing it right.
Here is different between using transform and rigidbody for movement
Transform set position give you flexibility of controlling speed and direction based on input like most of arcade, platform game.
Rigidbody is for more real feeling movement of human like that can be slow, accelerate as needed (most use for console game + fews 2D game need dynamic Physics) which need some more extra work.
You can only pick one way to control movement as one will break another. In most case, people go with transform set position since it is much simpler and no one notice the difference
Your answer
Follow this Question
Related Questions
My Unity Rigidbody 2D character fall slowly after a jump,Unity RigidBody Character falls slowly 0 Answers
Unity 2d random Enemy and random direction movement on spawn. 0 Answers
how to fix velocity for jump and movement or fall in 2d pltform game with gravity 4 Answers
Should i use rigidbody for platformer movement? 1 Answer
Overlapping two dynamic rigidbodies (2D) 0 Answers