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 /
avatar image
1
Question by HDNua · Aug 24, 2015 at 02:37 PM · slope

[2D] How can I fix player's slope moving problem?

Hi, I'm creating megaman-like 2D platformer game. I cannot solve how to fix my slope problem.

This image and movie imply my problem. alt text

https://www.youtube.com/watch?v=2dVgjvniJ3Q&feature=youtu.be

I think, first situation is occurred because there's y direction inertia. so when I stop my player but y velocity still remains and it results my character to fall in a short moment.

second situation is, when player moves to down, it floats a moment. That makes my character shows fall animation.

So I concludes that, both situations are occurred because my character is not attached to ground completely...

I want to make my project just like Megaman X series. That means, Player must be attached to slope when it is moving.

Gravity is 0, ground checking uses EdgeCollider2D and set layer as 'ground'

You can download my test project in here. https://drive.google.com/file/d/0B_piXOzIlhTsX3R2ajdpTnllb0U/view?usp=sharing

This is my player controller source.

 using UnityEngine;
 using System.Collections;
 
 /// <summary>
 /// Player Controller
 /// </summary>
 public class PlayerController : MonoBehaviour
 {
     #region Components list
     BoxCollider2D _collider;
     Rigidbody2D _rigidbody;
     Animator _animator;
 
     #endregion
 
 
 
     #region public fields Unity can accessible
     public float movingSpeed = 5;
     public LayerMask whatIsGround;
     public LayerMask whatIsWall;
 
     #endregion
 
 
 
     #region fields and property implies player's physical state
     bool landed = false;
     bool facingRight = true;
     bool moving;
 
     /// <summary>
     /// Is player grounded?
     /// </summary>
     bool Landed
     {
         get { return landed; }
         set { _animator.SetBool("Landed", landed = value); }
     }
     /// <summary>
     /// Is player moving?
     /// </summary>
     bool Moving
     {
         get { return moving; }
         set { _animator.SetBool("Moving", moving = value); }
     }
 
     #endregion
 
 
 
     #region overrides MonoBehaviour default methods
     void Start()
     {
         _rigidbody = GetComponent<Rigidbody2D>();
         _animator = GetComponent<Animator>();
         _collider = GetComponent<BoxCollider2D>();
     }
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.X))
         {
             _rigidbody.velocity = new Vector2(_rigidbody.velocity.x, 16);
         }
     }
     void FixedUpdate()
     {
         // check if player touches ground
         Landed = _collider.IsTouchingLayers(whatIsGround);
         if (Landed)
         {
 
         }
         else
         {
             // fall player if it is not on the ground
             float vy = _rigidbody.velocity.y - 0.8f;
             if (vy > 16) vy = 16;
             _rigidbody.velocity = new Vector2
                 (_rigidbody.velocity.x, vy);
         }
 
         // handle keyboard input
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             MoveLeft();
         }
         else if (Input.GetKey(KeyCode.RightArrow))
         {
             MoveRight();
         }
         else
         {
             StopMoving();
         }
     }
 
     #endregion
 
 
 
     #region assist methods
     /// <summary>
     /// move player to left
     /// </summary>
     void MoveLeft()
     {
         if (facingRight)
             Flip();
         _rigidbody.velocity = new Vector2(-movingSpeed, _rigidbody.velocity.y);
         Moving = true;
     }
     /// <summary>
     /// move player to right
     /// </summary>
     void MoveRight()
     {
         if (facingRight == false)
             Flip();
         _rigidbody.velocity = new Vector2(movingSpeed, _rigidbody.velocity.y);
         Moving = true;
     }
     /// <summary>
     /// stop player's moving
     /// </summary>
     void StopMoving()
     {
         _rigidbody.velocity = new Vector2(0, _rigidbody.velocity.y);
         Moving = false;
     }
 
     /// <summary>
     /// flip player's direction
     /// </summary>
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 newScale = transform.localScale;
         newScale.x *= -1;
         transform.localScale = newScale;
     }
 
     #endregion
 }


Sorry for my poor English, but thanks for reading.

제목-없음.png (9.2 kB)
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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Problem sliding down hills (WITH VIDEO) 1 Answer

Travel up and down slopes 0 Answers

Slope angle fix 0 Answers

How to rotate the Rigidbody based on the normal of the ground?? 1 Answer

Collision normal angles. 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