- Home /
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);
}
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.
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
Follow this Question
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