Hold shift to increase speed of the player. Since accessing the motor script from outside scripts seems to be difficult to do, create a new float in the playerController script that makes it so when shift is pressed down, the value equals 10. Then when shift is not being held down, make the float go back to the original speed 6. Change the motor script so that it is using the float from PlayerController instead of its own variable if possible. If not, research how to access and edit it. Maybe shift command needs to be in the motor script and it creates a new variable that overrides the current speed one.
Go to >Edit >Project Settings >Input. Once you are there, make the size under the axis text 16 or one more than it was. The default will be Jump, rename it to LeftShift.
if(Input.GetButton("Shift"))
{
sprintSpeed = 10;
}
else{
sprintSpeed = 6;
}
///////////////////////////////
using UnityEngine;
using System.Collections;
public class RunAndCrouch : MonoBehaviour
{
public float walkSpeed = 7; // regular speed
public float crchSpeed = 3; // crouching speed
public float runSpeed = 20; // run speed
private CharacterMotor chMotor;
private Transform tr;
private float dist; // distance to ground
// Use this for initialization
void Start ()
{
chMotor = GetComponent<CharacterMotor>();
tr = transform;
CharacterController ch = GetComponent<CharacterController>();
dist = ch.height/2; // calculate distance to ground
}
// Update is called once per frame
void FixedUpdate ()
{
float vScale = 1.0f;
float speed = walkSpeed;
if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && chMotor.grounded)
{
speed = runSpeed;
}
if (Input.GetKey("c"))
{ // press C to crouch
vScale = 0.5f;
speed = crchSpeed; // slow down when crouching
}
chMotor.movement.maxForwardSpeed = speed; // set max speed
float ultScale = tr.localScale.y; // crouch/stand up smoothly
Vector3 tmpScale = tr.localScale;
Vector3 tmpPosition = tr.position;
tmpScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5 * Time.deltaTime);
tr.localScale = tmpScale;
tmpPosition.y += dist * (tr.localScale.y - ultScale); // fix vertical position
tr.position = tmpPosition;
So to anyone that wants to make the crouching factor smaller than 0.5 I suggest you make the character controller radius smaller (example 0.2 for radius worked for me when crouching to 0.3f). This avoids the falling through the floor problem. Hope this helps someone :)
Hold shift to increase speed of the player. Since accessing the motor script from outside scripts seems to be difficult to do, create a new float in the playerController script that makes it so when shift is pressed down, the value equals 10. Then when shift is not being held down, make the float go back to the original speed 6. Change the motor script so that it is using the float from PlayerController instead of its own variable if possible. If not, research how to access and edit it. Maybe shift command needs to be in the motor script and it creates a new variable that overrides the current speed one.
Go to >Edit >Project Settings >Input. Once you are there, make the size under the axis text 16 or one more than it was. The default will be Jump, rename it to LeftShift.
if(Input.GetButton("Shift")) { sprintSpeed = 10; } else{ sprintSpeed = 6; } /////////////////////////////// using UnityEngine; using System.Collections;
public class RunAndCrouch : MonoBehaviour { public float walkSpeed = 7; // regular speed public float crchSpeed = 3; // crouching speed public float runSpeed = 20; // run speed
So to anyone that wants to make the crouching factor smaller than 0.5 I suggest you make the character controller radius smaller (example 0.2 for radius worked for me when crouching to 0.3f). This avoids the falling through the floor problem. Hope this helps someone :)