Rigidbody2D.MovePosition acting a bit strange
I have a 2D platformer game being made right now, and I'm trying to nail the player's movement currently. After a while of getting the player to move correctly and collide smoothly with walls using the MovePosition method, something funny has happened to my character's jumping. Before I implemented the MovePosition, I used transform.translate. This worked until I started testing it against wall collision and then I found out about the teleporting and jittering so I looked for other methods and found MovePosition (which works perfectly). Now this is where my problem comes in. Before I added in MovePosition, I was jumping fine. When the up arrow is pressed, force is applied to the rigidbody which pushes it up, the player then comes down to the ground as you'd expect. Now with the MovePosition, it does the same thing except when I try to move around in the air, the player sort of slowly glides around as if it almost wasn't affected by gravity. Here is a youtube video showing what it does (sorry for bad quality): https://www.youtube.com/watch?v=OxAYXlSm7BA
[1]: https://www.youtube.com/watch?v=OxAYXlSm7BA
Here is what my player move script looks like, its in C#.
public class MoveScript : MonoBehaviour {
private Rigidbody2D rb;
private bool grounded = true;
private bool hasJumped = false;
private Vector2 left = new Vector2(-1, 0);
private Vector2 right = new Vector2(1, 0);
private Vector2 up = new Vector2(0, 1);
public float speed;
public float jumpPower = 250;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
}
void FixedUpdate()
{
if (Input.GetKey("right"))
{
rb.MovePosition(rb.position + right * speed);
}
if (Input.GetKey("left"))
{
rb.MovePosition(rb.position + left * speed);
}
if (!grounded && rb.velocity.y == 0)
{
grounded = true;
}
if (Input.GetKeyDown("up") && grounded == true)
{
hasJumped = true;
}
if (hasJumped)
{
rb.AddForce(transform.up * jumpPower);
grounded = false;
hasJumped = false;
}
}
}
Any and all help would be appreciated about this issue. If you see anything else in the script which you think needs changing for other reasons, please let me know. I'm a pretty big beginner on unity and game dev.
Rigidbody2D is supposed to act like that. When a $$anonymous$$ovePosition is triggered, neither gravity or linear drag will affect the object. I am also still looking for a way to work around this, so I will get back to you when I find a way. :)
Your answer
Follow this Question
Related Questions
Left hand side of an assignment must be a variable, a property or an indexer 1 Answer
how do i create a platform(surface) for a object with rigidbody2d to stand on? 1 Answer
how would I change player.transform.RotateAround to MoveRotation 0 Answers
Move RigiBody2d character to mouse (touch) position 0 Answers
How to tidy up my code for Unity 5 Basic Platformer 1 Answer