The question is answered, right answer was accepted
2D Platformer: How to stop my character from auto-sliding?
Long story short, I am making a 2D platformer, and I have came across an error that I can't seem to find a solution too, no matter where I look. My character will automatically slide down ramps (all of which are at a 45 degree angle, if that means anything), and going up them is a bit slower than normally walking on flat ground. I just want to know how to make it so they won't slide. Here is my entire Player script, so you can see what I currently have for movement, so on and so forth:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour {
//Floats
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 200f;
//Booleans
public bool grounded;
public bool canDoubleJump;
public bool facingRight = true;
//Stats
public int curHealth;
public int maxHealth = 5;
//References
private Rigidbody2D rb2d;
private Animator anim;
private GameMaster gm;
void Start () {
rb2d = gameObject.GetComponent<Rigidbody2D> ();
anim = gameObject.GetComponent<Animator> ();
curHealth = maxHealth;
gm = GameObject.FindGameObjectWithTag ("GameMaster").GetComponent<GameMaster> ();
}
void Update () {
anim.SetBool ("Grounded", grounded);
anim.SetFloat ("Speed", Mathf.Abs(rb2d.velocity.x));
//Movement
if (Input.GetAxis ("Horizontal") < -0.1f) {
transform.localScale = new Vector3 (-1, 1, 1);
facingRight = false;
}
if (Input.GetAxis ("Horizontal") > 0.1f) {
transform.localScale = new Vector3 (1, 1, 1);
facingRight = true;
}
if (Input.GetButtonDown ("Jump")) {
if (grounded) {
rb2d.AddForce (Vector2.up * jumpPower);
canDoubleJump = true;
} else if (canDoubleJump) {
canDoubleJump = false;
rb2d.velocity = new Vector2 (rb2d.velocity.x, 0);
rb2d.AddForce (Vector2.up * jumpPower);
}
}
//Death
if (curHealth > maxHealth) {
curHealth = maxHealth;
} else if (curHealth <= 0) {
curHealth = 0;
Die ();
}
}
void FixedUpdate() {
// Fake friction / easing the X speed of the player
Vector3 easeVelocity = rb2d.velocity;
easeVelocity.y = rb2d.velocity.y;
easeVelocity.z = 0.0f;
easeVelocity.x *= 0.75f;
if (grounded) {
rb2d.velocity = easeVelocity;
}
// Moving the player
float h = Input.GetAxis ("Horizontal");
if (grounded) {
rb2d.AddForce ((Vector2.right * speed) * h);
} else {
rb2d.AddForce ((Vector2.right * speed / 2) * h);
}
// Limit speed of the player
if (rb2d.velocity.x > maxSpeed) {
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}
if (rb2d.velocity.x < -maxSpeed) {
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
}
// All the functions that deal with getting hurt.
void Die() {
SceneManager.LoadScene (SceneManager.GetActiveScene().name);
}
public void Damage(int dmg) {
curHealth -= dmg;
gameObject.GetComponent<Animation> ().Play ("Hurt");
}
public IEnumerator Knockback(float knockDur, float knockPwr, Vector3 knockDir) {
float timer = 0;
while (knockDur > timer) {
timer += Time.deltaTime;
rb2d.velocity = new Vector2 (0, 0);
rb2d.AddForce (new Vector3 (knockDir.x * -100, knockDir.y + knockPwr, transform.position.z)); // X: Opposite direction, Y: Knocked up depending on how powerful the knockback is, Z: No change.
}
yield return 0;
}
// Handles picking up Coins and Hearts
void OnTriggerEnter2D(Collider2D col) {
if (col.CompareTag ("Coin")) {
Destroy (col.gameObject);
gm.score += 1;
} else if (col.CompareTag ("Heart") && curHealth < maxHealth) {
Destroy (col.gameObject);
curHealth += 1;
}
}
}
Alternate link to the code: http://pastebin.com/Rz8Eiq8a
If you need more information to assist, please contact me or reply to this post and I will provide your requested info.
EDIT: Bumped this multiple times now, received no answers. If literally ANYONE has an idea, please tell me. This issue is really starting to get on my nerves. -.-
Answer by mohammad-alavi-74 · Aug 25, 2016 at 01:31 PM
try adding specific tags to your ramps, and when the player has entered these ramps (tell by using OnTriggerEnter), add an upwards force that would neutralize the gravity force and then add a virtual force in the downwards direction of the 45 degree floor (or add the vector sum of these two forces), or you can turn the gravity off and then add the virtual force, or any other measure that would change the direction of the gravity in that specific area....tell me how it works out... you can get help from this link: Change Gravity Direction
I did try this, added it to my GroundCheck script... And after an hour of tinkering, using the add forces and crap, as well as some of my own messing around, I finally managed to fix it. Only issue I have is that when my character is on a ramp, I had to make it so their velocity was immediately set to 0 when they weren't pressing any buttons. It's rough, but it works. I may ask on how to improve that in a different question.
TL;DR Thanks for pushing me in the right direction. I have figured it out, even though it's a bit rough. Thanks again. :P