Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CharacterController : MonoBehaviour
- {
- private float yaw = 0.0f, pitch = 0.0f;
- private Rigidbody rb;
- private bool isSprinting = false;
- float walkSpeed = 7.0f, sensitivity = 2.0f;
- void Start()
- {
- Cursor.lockState = CursorLockMode.Locked;
- rb = gameObject.GetComponent<Rigidbody>();
- }
- void Update()
- {
- if (Input.GetKey(KeyCode.Space) && Physics.Raycast(rb.transform.position, Vector3.down, 1 + 0.001f))
- rb.velocity = new Vector3(rb.velocity.x, 7.0f, rb.velocity.z);
- Look();
- }
- private void FixedUpdate()
- {
- Movement();
- }
- void Look()
- {
- pitch -= Input.GetAxisRaw("Mouse Y") * sensitivity;
- pitch = Mathf.Clamp(pitch, -90.0f, 90.0f);
- yaw += Input.GetAxisRaw("Mouse X") * sensitivity;
- Camera.main.transform.localRotation = Quaternion.Euler(pitch, yaw, 0);
- }
- void Movement()
- {
- Vector2 axis = new Vector2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")) * walkSpeed;
- Vector3 forward = new Vector3(-Camera.main.transform.right.z, 0.0f, Camera.main.transform.right.x);
- Vector3 wishDirection = (forward * axis.x + Camera.main.transform.right * axis.y + Vector3.up * rb.velocity.y);
- rb.velocity = wishDirection;
- if (Input.GetKey(KeyCode.LeftShift))
- {
- isSprinting = true;
- }
- else
- {
- isSprinting = false;
- }
- if (isSprinting == true)
- {
- walkSpeed = 10.0f;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment