JS
#pragma strict
var walkSpeed : float = 7; //Regular speed
var crouchSpeed : float = 3; //Speed while crouching
var sprintSpeed : float = 13; // Speed while sprinting
private var charMotor : CharacterMotor;
private var charController : CharacterController;
private var theTransform : Transform;
private var charHeight : float; //Initial height
function Start ()
{
charMotor = GetComponent(CharacterMotor);
theTransform = transform;
charController = GetComponent(CharacterController);
charHeight = charController.height;
}
function Update ()
{
var h = charHeight;
var speed = walkSpeed;
if (charController.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift"))
{
speed = sprintSpeed;
}
if (Input.GetKey("c"))
{
h = charHeight*0.5;
speed = crouchSpeed;
}
charMotor.movement.maxForwardSpeed = speed; // Setting the max speed
var lastHeight = charController.height; //Stand up/crouch smoothly
charController.height = Mathf.Lerp(charController.height, h, 5*Time.deltaTime);
theTransform.position.y += (charController.height-lastHeight)/2; //Fix vertical position
}
C#
using UnityEngine;
using System.Collections;
public class SprintAndCrouch : MonoBehaviour {
public float walkSpeed = 7;
public float runSpeed = 13;
public float crouchSpeed = 3;
private float speed;
//Stamina Variables
public int stamina;
public int maxStamina = 100;
private bool canRun = true;
private CharacterController charControl;
private CharacterMotor charMotor;
private Transform theTransform;
private float charHeight;
// Use this for initialization
void Start ()
{
charMotor = GetComponent <CharacterMotor>();
charControl = GetComponent <CharacterController>();
theTransform = new GameObject().transform;
charHeight = charControl.height;
}
// Update is called once per frame
void Update ()
{
speed = walkSpeed;
Sprint ();
Crouch ();
}
void Sprint()
{
int fatigueAdd = 1;
if (Input.GetKey (KeyCode.LeftShift) && charControl.isGrounded && stamina <= maxStamina && canRun == true)
{
canRun = true;
speed = runSpeed;
stamina = stamina + fatigueAdd;
//Walks if stamina is greater than max stamina
if(stamina >= maxStamina)
{
speed = walkSpeed;
canRun = false;
}
}
//If not running decrement stamina
else if(stamina > 0)
{
stamina -= 1;
//Set canRun to true if stamina hits 0
if(stamina == 0)
canRun = true;
}
charMotor.movement.maxForwardSpeed = speed;
charMotor.movement.maxSidewaysSpeed = speed;
}
void Crouch()
{
float h = charHeight;
if(Input.GetKey(KeyCode.C))
{
h = charHeight / 2;
speed = crouchSpeed;
}
charMotor.movement.maxForwardSpeed = speed;
charMotor.movement.maxSidewaysSpeed = speed;
float lastHeight = charControl.height;
charControl.height = Mathf.Lerp (charControl.height, h, Time.deltaTime * 5);
//theTransform.position.y += (charControl.height - lastHeight) / 2;
//Vector3 temp = new Vector3(transform.position.x, charControl.height - lastHeight/2, transform.position.z);
//Bug where the player falls out of the plane/ground
Vector3 temp = Vector3.zero;
temp.y = (charControl.height - lastHeight) / 2;
theTransform.position += temp;
}
}
Are this lines of code the same.
Since in the C# script there’s a bug where the Player is gonna fall of the plane/ground while the JS is perfectly fine and doesn’t produce a bug like in the C# script.