Question by
crabcrabcam · Sep 23, 2016 at 10:01 AM ·
movement scriptitween
How do I stop my character feeling like they're on ice using RigidBody2D Velocity
I am currently using RB2D velocity for movement but I can't make it stop sliding after I've finished pressing the button. It feels like the Ice level from Mario. How do I stop my character sliding everywhere?
My code is:
void Movement() {
if (Input.GetAxisRaw ("Horizontal") > 0.1) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);
}
if (Input.GetAxisRaw ("Horizontal") < -0.1) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-speed, GetComponent<Rigidbody2D> ().velocity.y);
}
if (Input.GetAxisRaw ("Vertical") > 0.5 && grounded) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jumpHeight);
}
}
My speed is set to 5, jumpHeight is 10 and I have both box collider and RigidBody2D on my player
Thanks for any help in advance
Comment
Best Answer
Answer by crabcrabcam · Sep 23, 2016 at 10:31 AM
Fixed this thanks to cjf93 on StackOverflow
New code is
void Movement() {
if (Input.GetAxisRaw ("Horizontal") > 0.1) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);
} else if (Input.GetAxisRaw ("Horizontal") < -0.1) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-speed, GetComponent<Rigidbody2D> ().velocity.y);
} else if (Input.GetAxisRaw("Horizontal") == 0 && grounded) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, GetComponent<Rigidbody2D> ().velocity.y);
}
if (Input.GetAxisRaw ("Vertical") > 0.5 && grounded) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jumpHeight);
}
}
This makes the character floaty in the air and stay still whilst on the ground after you stop pressing the button :)
Your answer
Follow this Question
Related Questions
iTween Path and Samsung Gear VR 3 Answers
Movement Problem 1 Answer
SImple, but how do I check a game object's position in an If statement? 1 Answer