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
0
Question by Deathslice · Dec 27, 2017 at 07:21 PM · gui2d-platformerphysics2dslopeedge

How to Handle Corners on a Slope in a 2D Platformer Game?

So I've been following a unity live training session and integrated the code that they wrote into my 2D platformer game. After doing so, I came across a slight problem and it goes like this. I have a 1D blend tree in my animator controller that plays animations(jumping and falling animations) based on the vertical speed of the character. What happens when the character hits the edge of a slope is that it changes the vertical speed of character and plays one of the falling animations in the blend tree instead of the player just moving up or down the slope. Here is an image of the player executing its jumping animation as it touches the start of the slope.

Problem Screenshot text

And here is the relevant code from the training session.

 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody2D))]
 public class BaseEntityController : MonoBehaviour
 {
     [SerializeField]private float gravityModifier = 2f;
     [SerializeField]private float minGroundNormalY = 0.65f;
 
     protected Vector2 velocity;
     protected Vector2 targetVelocity;
     protected Rigidbody2D rb2D;
     protected ContactFilter2D contactFilter;
     protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
     protected List<RaycastHit2D> hitBufferList = new List<RaycastHit2D>(16);
 
     protected const float minMoveDistance = 0.001f;
     protected const float shellRadius = 0.01f;
 
     protected bool grounded;
     protected Vector2 groundNormal;
 
     private void OnEnable()
     {
         rb2D = GetComponent<Rigidbody2D>();
     }
 
     private void Start()
     {
         contactFilter.useTriggers = false;
         contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
         contactFilter.useLayerMask = true;
     }
 
     private void Update()
     {
         targetVelocity = Vector2.zero;
         ComputeVelocity();
     }
 
     protected virtual void ComputeVelocity()
     {
 
     }
 
     private void FixedUpdate()
     {
         velocity += gravityModifier * Physics2D.gravity * Time.fixedDeltaTime;
         velocity.x = targetVelocity.x;
 
         grounded = false;
 
         Vector2 deltaPosition = velocity * Time.fixedDeltaTime;
         Vector2 horizontalMovement = new Vector2(groundNormal.y, -groundNormal.x);
         Vector2 move = horizontalMovement * deltaPosition.x;
 
         Movement(move, false);
 
         move = Vector2.up * deltaPosition.y;
 
         Movement(move, true);
     }
 
     private void Movement(Vector2 move, bool yMovement)
     {
         float distance = move.magnitude;
 
         // This if statement checks to see if it is possible for us to collide with something in the next frame.
         if(distance > minMoveDistance)
         {
             int totalColliders = rb2D.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
 
             hitBufferList.Clear();
 
             for (int i = 0; i < totalColliders; i++)
             {
                 hitBufferList.Add(hitBuffer[i]);
             }
 
             for (int i = 0; i < hitBufferList.Count; i++)
             {
                 Vector2 currentNormal = hitBufferList[i].normal;
 
                 if(currentNormal.y > minGroundNormalY)
                 {
                     grounded = true;
 
                     if(yMovement)
                     {
                         groundNormal = currentNormal;
                         currentNormal.x = 0;
                     }
                 }
 
                 float projection = Vector2.Dot(velocity, currentNormal);
 
                 if(projection < 0.0f)
                 {
                     velocity = velocity - projection * currentNormal;
                 }
 
                 float modifiedDistance = hitBufferList[i].distance - shellRadius;
                 distance = modifiedDistance < distance ? modifiedDistance : distance;
             }
         }
 
         rb2D.position = rb2D.position + move.normalized * distance;
     }
 }

So from what I can understand, this section of code

   float projection = Vector2.Dot(velocity, currentNormal);
 
    if(projection < 0.0f)
    {
          velocity = velocity - projection * currentNormal;
    }

is what slows down the velocity vector only when the projection value obtained from the dot product of the velocity vector and the normal vector that the player collides with is negative(in other words both vectors are opposites each other such as when a player hits a wall or a ceiling). I've tried modifying the vertical speed threshold in the blend tree but that didn't help. How can I make it so that the player doesn't start playing one of it's falling animations the moment it starts running up and down the slope?

screenshot-122.png (346.0 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

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

Jagged white edges around partly transparent textures in GUI 2 Answers

Camera following Rigidbody jitter every few seconds with background 0 Answers

Bumper physics not working, 1 Answer

How should I rotate my character on a slope? (2d game) 0 Answers

While Moving Left or Right my character falls more slowly. 2 Answers


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