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 SmileyFaceOfRom · Aug 21, 2012 at 11:38 PM · rigidbodystairsramp

Stop rigidbody from sliding down "stairs".

EDIT I added your suggestion and it stops me, but like you said, I get frozen. Now this next part of script allows me to see when my WASD buttons are being pressed, and it worked without any issues.

 if(!Input.GetButton("Horizontal"))
     {    
         //Debug.Log("There is no horizontal input.");
         if(!Input.GetButton("Vertical"))
         {    
             //Debug.Log("There is no Vertical input.");
             moveInput = false;
         }
     }    
     //If there IS WASD input, moveInput = true.
     if(Input.GetButton("Horizontal") == true)
     {    
         //Debug.Log("There IS horizontal input.");
         moveInput = true;
     }
     if(Input.GetButton("Vertical") == true)
     {    
         //Debug.Log("There IS vertical input.");
         moveInput = true;
     }    

This next part stops me just fine, but it won't let me restart.

 if(collision.gameObject.tag =="StairsPlane")
     {
         //Debug.Log("In contact w/stairs plane.");
         if(moveInput == false)
             rigidbody.constraints = RigidbodyConstraints.FreezePosition;
         else
             rigidbody.constraints = RigidbodyConstraints.FreezeNone;
     }


I'm using a rigidbody for my character. My stairs consist of a ramp that the character "walks up". My issue is that he slides down the ramp when I stop moving. Can anyone offer me some assistance on how to stop this?

PlayerMovementScript.js
var walkAcceleration : float = 5; var walkAccelAirRacio : float = 0.1; var walkDeacceleration : float = 5; @HideInInspector var walkDeaccelerationVolx : float; @HideInInspector var walkDeaccelerationVolz : float;

 var cameraObject : GameObject;
 var maxWalkSpeed : float = 20;
 @HideInInspector
 var horizontalMovement : Vector2;
 var jumpVelocity : float = 20;
 @HideInInspector
 var grounded : boolean = false;
 var maxSlope : float = 60;
 
 //crouching
 var crouchRacio : float = 0.3;
 var transitionToCrouchSec : float = 0.2;
 @HideInInspector
 var crouchVelocity : float;
 var currentCrouchRacio : float = 1;
 @HideInInspector
 var originalLocalScaleY : float;
 @HideInInspector
 var crouchLocalScaleY : float;
 @HideInInspector
 var crouchingSpeed : float;
 @HideInInspector
 var maxWalkSpeedOrig : float;
 
 //end crouching and collision detection
 var collisionDetectionSphere : GameObject;
 
 //switch guns
 @HideInInspector
 var waitFrameForSwitchGuns : int = -1;
 var currentGun : GameObject;
 var distToPickUpGun : float =6;
 var throwGunUpForce : float = 100;
 var throwGunForwardForce : float = 300;
 
 //Pickup Ammo
 @HideInInspector
 var waitToPickupAmmo : float = 0;
 
 //Position for AIPathCell
 var currentCell : GameObject;
 
 
 function Awake ()
 {
     maxWalkSpeedOrig = maxWalkSpeed;
     currentCrouchRacio = 1;
     originalLocalScaleY = transform.localScale.y;
     crouchLocalScaleY = transform.localScale.y * crouchRacio;
     crouchingSpeed = maxWalkSpeed / 10;
 }
 
 function Update () 
 {
     waitFrameForSwitchGuns -= 1;
     waitToPickupAmmo -= Time.deltaTime;
     
     transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRacio);
     if(Input.GetButton("Crouch"))
         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 0, crouchVelocity, transitionToCrouchSec);
         maxWalkSpeed = crouchingSpeed;
         
     if(Input.GetButton("Crouch") == false && collisionDetectionSphere.GetComponent(CollisionDetectionSphereScript).collisionDetected == false)
         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 1, crouchVelocity, transitionToCrouchSec);
         maxWalkSpeed = crouchingSpeed;
     if(!Input.GetButton("Crouch"))
         maxWalkSpeed = maxWalkSpeedOrig;
 
     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
     if (horizontalMovement.magnitude > maxWalkSpeed)
     {
         horizontalMovement = horizontalMovement.normalized;
         horizontalMovement *= maxWalkSpeed;
     }
     rigidbody.velocity.x = horizontalMovement.x;
     rigidbody.velocity.z = horizontalMovement.y;
     
     if (grounded){
         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);
     }
     
     
     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
 
 //Is jumping, less movement control
     /*
     if(grounded)    
         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
     else
         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
     */
     
     if(Input.GetButtonDown("Jump") && grounded)
         rigidbody.AddForce(0, jumpVelocity,0);
 }
 
 function FixedUpdate ()
 {
     if(grounded)    
         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
     else
         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio);
     
 }
 
 
 function OnCollisionStay (collision: Collision)
 {
     for (var contact : ContactPoint in collision.contacts)
     {
         if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
             grounded = true;
     }
 }
 
 function OnCollisionExit ()
 {
  grounded = false;
 }
 
 
 function OnTriggerStay (hitTrigger : Collider)
 {
 //Set current cell for Ai pathfinding
     if(hitTrigger.tag == "AIPathCell")
         currentCell = hitTrigger.gameObject;
 
 //Handles stairs up & down
     if (hitTrigger.transform.tag == "StairGoingUp")
 
         if (!Input.GetButton("Jump") && Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
 
             if (rigidbody.velocity.y > 0)
 
                 rigidbody.velocity.y = 0;
 
     if (hitTrigger.transform.tag == "StairGoingDown")
 
         if (!Input.GetButton("Jump") && Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
 
             rigidbody.AddForce(0,-100,0);
 
 

}

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

· Add your reply
  • Sort: 
avatar image
2

Answer by OperationDogBird · Aug 22, 2012 at 12:27 AM

You could use rigidbody restraints for the position while there is no input. Something like

 if(!moveInput)rigidbody.contraints=RigidbodyConstraints.FreezePosition;

you could make it only for stairs by adding an isTrigger area on the stairs. You need to make sure to unfreeze once there is input, otherwise you will be locked in position.

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 BindingForceDev · Feb 18, 2016 at 10:24 PM 0
Share

Thanks for this suggestion! Here is the code I used to stop sliding movement, while still using it's advantages. Basically moving is true if there is input from the user, ai controlled movement, or movement effects from abilities... This code allows the player to be affected by the terrain collider when moving, so slopes are still harder to climb without a high movement velocity.

 if (moving) {
     gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
     gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
 } else {
     gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
 }

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

9 People are following this question.

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

Related Questions

Rigidbodys and stairs/ramps 1 Answer

Collision If Statement And Falling throught the ground (2 questions) 1 Answer

rotation problem 1 Answer

Creating a teleportation gun 1 Answer

How to disable flying ? 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