using System.Collections; using System.Collections.Generic; using UnityEngine; using MLAPI; public class PlayerController : NetworkBehaviour { public Transform playerCamera = null; public CharacterController playerController = null; public float mouseSensitivity = 3.5f; public float speed = 10f; public float gravity = -13f; public float mouseSmoothTime = 0.03f; public float moveSmoothTime = 0.3f; float cameraPitch = 0.0f; float velocityY = 0.0f; Vector2 currentMouseDelta = Vector2.zero; Vector2 currentMouseDeltaVelocity = Vector2.zero; Vector2 currentDirection = Vector2.zero; Vector2 currentDirectionVelocity = Vector2.zero; // Start is called before the first frame update void Start() { Cursor.lockState = CursorLockMode.Locked; if (!IsLocalPlayer) { playerCamera.GetComponent().enabled = false; playerCamera.GetComponent().enabled = false; } } // Update is called once per frame void Update() { if (IsLocalPlayer) { UpdateMouseLook(); UpdateMovement(); if (Input.GetKey(KeyCode.LeftShift)) { speed = 20f; } else { speed = 10f; } if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } } } void UpdateMouseLook() { Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime); cameraPitch -= currentMouseDelta.y * mouseSensitivity; cameraPitch = Mathf.Clamp(cameraPitch, -90f, 90f); playerCamera.localEulerAngles = Vector3.right * cameraPitch; transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity); } void UpdateMovement() { Vector2 targetDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); targetDirection.Normalize(); currentDirection = Vector2.SmoothDamp(currentDirection, targetDirection, ref currentDirectionVelocity, moveSmoothTime); if (playerController.isGrounded) { velocityY = 0.0f; } velocityY += gravity * Time.deltaTime; Vector3 velocity = (transform.forward * currentDirection.y + transform.right * currentDirection.x) * speed + Vector3.up * velocityY; playerController.Move(velocity * Time.deltaTime); } }