Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 snipedavid22 · Sep 25, 2020 at 10:03 AM · movementrigidbody3d

RigidBody falls slowly when coliding with stairs

Hi, I am trying to make a RigidBody movement in my 3d game and I did a script that makes the player climb stiars but I sometimes get a problem when the player gets stucks and slowly goes back.

This is my script: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class PlayerMovement2 : MonoBehaviour
 {
     public float speed;
     public Rigidbody rb; 
     public float jumpForce;
     [Header("Steps")]
     public float maxStepHeight = 0.4f;              
     public float stepSearchOvershoot = 0.01f;
 
     private List<ContactPoint> allCPs = new List<ContactPoint>();
     private Vector3 lastVelocity;
 
     void FixedUpdate()
     {
         #region Stairs
         Vector3 velocity = this.GetComponent<Rigidbody>().velocity;
         ContactPoint groundCP = default(ContactPoint);
         bool grounded = FindGround(out groundCP, allCPs);
 
         Vector3 stepUpOffset = default(Vector3);
         bool stepUp = false;
         if (grounded)
             stepUp = FindStep(out stepUpOffset, allCPs, groundCP, velocity);
         if (stepUp)
         {
             this.GetComponent<Rigidbody>().position += stepUpOffset;
             this.GetComponent<Rigidbody>().velocity = lastVelocity;
         }
         allCPs.Clear();
         lastVelocity = velocity;
         #endregion
     }
 
     void OnCollisionEnter(Collision col)
     {
         allCPs.AddRange(col.contacts);
     }
     void OnCollisionStay(Collision col)
     {
         allCPs.AddRange(col.contacts);
     }
     #region Stairs
     bool FindGround(out ContactPoint groundCP, List<ContactPoint> allCPs)
     {
         groundCP = default(ContactPoint);
         bool found = false;
         foreach (ContactPoint cp in allCPs)
         {
             //Pointing with some up direction
             if (cp.normal.y > 0.0001f && (found == false || cp.normal.y > groundCP.normal.y))
             {
                 groundCP = cp;
                 found = true;
             }
         }
 
         return found;
     }
     bool FindStep(out Vector3 stepUpOffset, List<ContactPoint> allCPs, ContactPoint groundCP, Vector3 currVelocity)
     {
         stepUpOffset = default(Vector3);
 
         //No chance to step if the player is not moving
         Vector2 velocityXZ = new Vector2(currVelocity.x, currVelocity.z);
         if (velocityXZ.sqrMagnitude < 0.0001f)
             return false;
 
         foreach (ContactPoint cp in allCPs)
         {
             bool test = ResolveStepUp(out stepUpOffset, cp, groundCP);
             if (test)
                 return test;
         }
         return false;
     }
     bool ResolveStepUp(out Vector3 stepUpOffset, ContactPoint stepTestCP, ContactPoint groundCP)
     {
         stepUpOffset = default(Vector3);
         Collider stepCol = stepTestCP.otherCollider;
 
         //( 1 ) Check if the contact point normal matches that of a step (y close to 0)
         if (Mathf.Abs(stepTestCP.normal.y) >= 0.01f)
         {
             return false;
         }
 
         //( 2 ) Make sure the contact point is low enough to be a step
         if (!(stepTestCP.point.y - groundCP.point.y < maxStepHeight))
         {
             return false;
         }
 
         //( 3 ) Check to see if there's actually a place to step in front of us
         //Fires one Raycast
         RaycastHit hitInfo;
         float stepHeight = groundCP.point.y + maxStepHeight + 0.0001f;
         Vector3 stepTestInvDir = new Vector3(-stepTestCP.normal.x, 0, -stepTestCP.normal.z).normalized;
         Vector3 origin = new Vector3(stepTestCP.point.x, stepHeight, stepTestCP.point.z) + (stepTestInvDir * stepSearchOvershoot);
         Vector3 direction = Vector3.down;
         if (!(stepCol.Raycast(new Ray(origin, direction), out hitInfo, maxStepHeight)))
         {
             return false;
         }
 
         //We have enough info to calculate the points
         Vector3 stepUpPoint = new Vector3(stepTestCP.point.x, hitInfo.point.y + 0.0001f, stepTestCP.point.z) + (stepTestInvDir * stepSearchOvershoot);
         Vector3 stepUpPointOffset = stepUpPoint - new Vector3(stepTestCP.point.x, groundCP.point.y, stepTestCP.point.z);
 
         //We passed all the checks! Calculate and return the point!
         stepUpOffset = stepUpPointOffset;
         return true;
     }
     #endregion
 
     void Start()
     {
         rb.GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         rb.velocity = new Vector3(Input.GetAxis("Horizontal") * speed, rb.velocity.y, Input.GetAxis("Vertical") * speed);
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
         }
     }
 }

alt text

here is a video: https://youtu.be/NKWyUVDamxM

screenshot-18.png (15.3 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

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

Character moving in the wrong direction, glitched Rigidbody? 1 Answer

Player getting stuck in ground (3D) player has Rigidbody, and Box Collider, world is Mesh Colliders 0 Answers

Problem with movement 0 Answers

Rigidbody grid based movement in a 3D game 0 Answers

Character faces wrong way for one frame only. Why? 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