| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Movement : MonoBehaviour {
- CharacterController mController;
- Vector3 velocity;
- public float Gravity;
- public float moveSpeed;
- public float jumpHeight=10;
- public float lookSensitivity;
- Camera cam;
- Vector3 rot;
- float defaultHeight;
- public float crouchHeight;
- public float crouchSpeed;
- bool crouch=true;
-
- public enum myEnum
- {
- hold,
- toggle
- };
- public myEnum crouchType;
- // Use this for initialization
- void Start () {
- mController = GetComponent<CharacterController>();
- cam = GetComponentInChildren<Camera>();
- defaultHeight = mController.height;
- }
-
- // Update is called once per frame
- void Update () {
- Move();
- GravityControl();
- //Jump();
- Crouch();
-
-
- }
- private void Move()
- {
- if (Input.GetKey(KeyCode.W))
- mController.Move(transform.forward*moveSpeed*Time.deltaTime);
- if (Input.GetKey(KeyCode.S))
- mController.Move(-transform.forward * moveSpeed * Time.deltaTime);
- if (Input.GetKey(KeyCode.A))
- mController.Move(-transform.right * moveSpeed * Time.deltaTime);
- if (Input.GetKey(KeyCode.D))
- mController.Move(transform.right * moveSpeed * Time.deltaTime);
- }
- void GravityControl()
- {
-
-
- if (!mController.isGrounded)
- {
- velocity.y += Gravity * Time.deltaTime;
- }
- mController.Move(velocity * Time.deltaTime);
- }
- void Jump()
- {
- /*if (Input.GetKeyDown(KeyCode.Space)&&mController.isGrounded)
- {
- velocity.y += jumpHeight * Time.deltaTime;
- }*/
- }
-
- void Crouch()
- {
- if (crouchType.ToString()=="hold")
- {
- if (Input.GetKey(KeyCode.C))
- {
- mController.height = Mathf.Lerp(mController.height, crouchHeight, crouchSpeed * Time.deltaTime);
- }
- else
- {
- mController.height = Mathf.Lerp(mController.height, defaultHeight, crouchSpeed * Time.deltaTime);
- }
- }
- if (crouchType.ToString() == "toggle")
- {
-
- if (Input.GetKeyDown(KeyCode.C))
- {
- crouch = !crouch;
- }
- if (crouch)
- {
- mController.height = Mathf.Lerp(mController.height,crouchHeight,crouchSpeed*Time.deltaTime);
- }
- else
- {
- mController.height = Mathf.Lerp(mController.height, defaultHeight, crouchSpeed * Time.deltaTime);
- }
- }
-
- }
-
- }
|