Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Sep 13, 2018 at 03:30 PM by Slayer3201 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Slayer3201 · Aug 18, 2016 at 02:03 PM · 2dplatformersliding

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Slayer3201 · Aug 27, 2016 at 07:56 AM 0
Share

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

Follow this Question

Answers Answers and Comments

94 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gradually rotate to slope angle 3 Answers

Should i use rigidbody for platformer movement? 1 Answer

Rigidbody2D falls slowly with MovePosition? 1 Answer

OnTriggerExit2D called repeatedly 0 Answers

Why is it that some sprites render in game view and others do not? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges