for some reason my player can jump infinitely when you continually press space. I have made a grounded variable but it doesn’t work. any ideas? ![]()
var walkAceleration: float = 5.0f;
var walkAccelerationAirRatio : float = 0.1;
var walkDeacceleration : float = 5;
@HideInInspector
var cameraObject : GameObject;
var rb : Rigidbody;
@HideInInspector
var horizontalMovement : Vector2;
var jumpVelocity : float = 20;
@HideInInspector
var grounded : boolean = false;
var maxSlope : float = 60;
var crouchRatio : float = 0.3;
var transitionToCrouchSec : float = 0.2;
var crouchingVelocity : float;
var currentCrouchRatio : float = 1;
var originalLocalScaleY : float;
var crouchLocalScaleY : float;
var collisionDetectionSphere : GameObject;
function Awake()
{
currentCrouchRatio = 1;
originalLocalScaleY = transform.localScale.y;
crouchLocalScaleY = transform.localScale.y * crouchRatio;
}
function Start()
{
rb = GetComponent.<Rigidbody>();
}
function Update ()
{
transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRatio);
if (Input.GetButton("Crouch"))
currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 0, crouchingVelocity, transitionToCrouchSec);
if (Input.GetButton("Crouch") == false && collisionDetectionSphere.GetComponent(CollisionDetectionSphereScript).collisionDetected == false)
currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 1, crouchingVelocity, transitionToCrouchSec);
GetComponent.<Rigidbody>().velocity.z = Mathf.Clamp(GetComponent.<Rigidbody>().velocity.z, -20, 20);
GetComponent.<Rigidbody>().velocity.x = Mathf.Clamp(GetComponent.<Rigidbody>().velocity.x, -20, 20); //Change the -10 and the 10 to alter movement speed
if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && grounded){
GetComponent.<Rigidbody>().velocity.x /= walkDeacceleration;
GetComponent.<Rigidbody>().velocity.z /= walkDeacceleration;}
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
if (grounded)
rb.AddRelativeForce(Input.GetAxis("Horizontal")* walkAceleration, 0, Input.GetAxis("Vertical")* walkAceleration);
else
rb.AddRelativeForce(Input.GetAxis("Horizontal")* walkAceleration * walkAccelerationAirRatio, 0, Input.GetAxis("Vertical")* walkAceleration * walkAccelerationAirRatio);
if (Input.GetButtonDown("Jump"))
GetComponent.<Rigidbody>().AddForce(0,jumpVelocity,0);
}
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;
}