Hi everyone I have been looking for some help with editing my movement script so that my player can crouch. I have no luck in editing. Im still learning a bit with scripting. Do you guys think you can help me out. Its in c#.Here is my movement script:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float walkSpeed = 5.0f;
public float jumpSpeed = 5.0f;
public float gravity = 20.0f;
public float run = 2.0f;
public float crouchSpeed = 0.5f;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
transform.position = new Vector3(0,1.7f,0);
}
void Update()
{
CharacterController controller = GetComponent<CharacterController> ();
if (controller.isGrounded) {
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= walkSpeed;
if (Input.GetButton ("Jump"))
moveDirection.y = jumpSpeed;
if (Input.GetKey (KeyCode.LeftShift))
moveDirection *= run;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
if (Input.GetButton ("Crouch")) {
moveDirection *= crouchSpeed;
}
}
}