So I have an FPS script that I’m working on and it prevents you from standing up if you are crouched under something that is too low (that way you can’t glitch through the floor or ceiling if you try to stand up) The code might look a bit intimidating (at least it always does to me, even tho i wrote it) but if someone can figure out why the crouching is weird that’d be awesome. Btw, if holding down crouch works fine, that’s not the problem
I think you must have forgotten to post some code
Oh my god your right!
var acceleration : float = 2000;
var deacceleration : float = 0.15;
var inAirAcceleration : float = 0.1;
var walkSpeed : float = 10;
var crouchSpeed : float = 5.0;
var standToCrouchTime : float = 0.2;
var crouchHeight : float = 0.3;
var jumpStrength : float = 3;
var maxClimbingAngle : float = 60;
var playerCamera : GameObject;
private var hor : float;
private var ver : float;
private var grounded : boolean = false;
private var xzMovement : Vector2;
private var deaccelVelX : float;
private var deaccelVelZ : float;
private var startScaleY : float;
private var crouching : boolean;
private var crouchScaleY : float;
private var currentCrouchRatio : float = 1;
private var crouchVel : float;
private var capsule : CapsuleCollider;
private var tdt : float;
function Awake ()
{
capsule = this.GetComponent(CapsuleCollider);
currentCrouchRatio = 1;
startScaleY = transform.localScale.y;
crouchScaleY = transform.localScale.y * crouchHeight;
}
function Update ()
{
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
tdt = Time.deltaTime;
}
function LateUpdate ()
{
transform.localScale.y = Mathf.Lerp(crouchScaleY, startScaleY, currentCrouchRatio);
if (Input.GetButton("Crouch"))
Crouch ();
else if(CanStand())
Stand ();
else Crouch ();
xzMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(crouching)
{
if(xzMovement.magnitude > crouchSpeed)
{
xzMovement = xzMovement.normalized;
xzMovement *= crouchSpeed;
}
}
else
{
if(xzMovement.magnitude > walkSpeed)
{
xzMovement = xzMovement.normalized;
xzMovement *= walkSpeed;
}
}
rigidbody.velocity.x = xzMovement.x;
rigidbody.velocity.z = xzMovement.y;
transform.rotation = Quaternion.Euler(0, playerCamera.GetComponent(MouseLook).currentYRotation, 0);
if (grounded)
{
rigidbody.AddRelativeForce(hor * acceleration * tdt, 0, ver * acceleration * tdt);
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, deaccelVelX, deacceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, deaccelVelZ, deacceleration);
if (Input.GetKeyDown(KeyCode.Space) && grounded)
if(!crouching)
rigidbody.AddForce(0, jumpStrength * 100, 0);
else rigidbody.AddForce(0, jumpStrength * 50, 0);
}
else rigidbody.AddRelativeForce(hor * acceleration * inAirAcceleration * tdt, 0, ver * acceleration * inAirAcceleration * tdt);
}
function OnCollisionStay (col : Collision)
{
for (var contact : ContactPoint in col.contacts)
if (Vector3.Angle(contact.normal, Vector3.up) < maxClimbingAngle)
grounded = true;
}
function OnCollisionExit ()
{
grounded = false;
}
function Crouch ()
{
currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 0, crouchVel, standToCrouchTime);
crouching = true;
}
function Stand ()
{
currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 1, crouchVel, standToCrouchTime);
crouching = false;
}
function CanStand () : boolean
{
var minCeilingHeight = capsule.height - crouchHeight;
if(Physics.Raycast(this.transform.position, Vector3.up, minCeilingHeight))
return false;
else return true;
}