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:
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. -.-