Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class CharacterController : MonoBehaviour
- {
- public float mouseSpeedX;
- public float mouseSpeedY;
- public float playerMoveSpeed;
- public float jumpForce;
- float verticalLookRotation;
- Rigidbody rb;
- Ray ray;
- bool isGrouded() {
- Vector3 pos = transform.position; pos.y -= 0.98f;
- Collider[] cols = Physics.OverlapSphere(pos, 0.2f);
- if (cols.Length > 1) return true;
- else return false;
- }
- void Start() {
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- rb = GetComponent<Rigidbody>();
- }
- void Update() {
- if (Input.GetKeyDown(KeyCode.Escape)) {
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- }
- if (Input.GetMouseButtonDown(0)) {
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- Camera.main.transform.localPosition = new Vector3(0f, 0.75f, 0f);
- transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * Time.deltaTime * mouseSpeedX);
- verticalLookRotation += Input.GetAxis("Mouse Y") * Time.deltaTime * mouseSpeedY;
- verticalLookRotation = Mathf.Clamp(verticalLookRotation, -90, 75);
- Camera.main.transform.localEulerAngles = Vector3.left * verticalLookRotation;
- rb.velocity = new Vector3(0f, rb.velocity.y, 0f);
- float finalMoveSpeed = (Input.GetKey(KeyCode.LeftShift)) ? playerMoveSpeed * 1.5f : playerMoveSpeed;
- Vector3 movement = transform.TransformDirection(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")).normalized);
- rb.MovePosition(transform.position + movement * finalMoveSpeed);
- if (Input.GetKey(KeyCode.Space) && isGrouded()) rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement