Untitled

 avatar
unknown
plain_text
2 years ago
3.6 kB
3
Indexable

using System.Collections; using System.Collections.Generic; using UnityEngine; using Mirror; using UnityEngine.SceneManagement; public class FPScontroller : MonoBehaviour { public float walkingSpeed = 7.5f; public float runningSpeed = 11.5f; public float jumpSpeed = 8.0f; public float gravity = 20.0f; public Camera playerCamera; public float lookSpeed = 2.0f; public float lookXLimit = 45.0f; CharacterController characterController; Vector3 moveDirection = Vector3.zero; float rotationX = 0; float rotationY = 0; [HideInInspector] public bool canMove = true; [SerializeField] public Animator animator; public bool Crouch; private float CrouchSpeedWalking; //Multiplayer public GameObject PlayerModel; void Start() { } void Update() { if (SceneManager.GetActiveScene().name == "Game") { if (PlayerModel.activeSelf == false) { characterController = GetComponent<CharacterController>(); // Lock cursor Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; CrouchSpeedWalking = walkingSpeed / 2; SetPosition(); PlayerModel.SetActive(true); } Debug.Log(Input.GetAxis("Horizontal")); if (Input.GetKeyDown(KeyCode.LeftControl)) { if (Crouch == false) { animator.SetLayerWeight(animator.GetLayerIndex("Crouch"), 1f); walkingSpeed = CrouchSpeedWalking; Crouch = true; } } if (Input.GetKeyUp(KeyCode.LeftControl)) { if (Crouch == true) { animator.SetLayerWeight(animator.GetLayerIndex("Crouch"), 0f); walkingSpeed = CrouchSpeedWalking * 2; Crouch = false; } } // We are grounded, so recalculate move direction based on axes Vector3 forward = transform.TransformDirection(Vector3.forward); Vector3 right = transform.TransformDirection(Vector3.right); // Press Left Shift to run bool isRunning = Input.GetKey(KeyCode.LeftShift); float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0; float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0; float movementDirectionY = moveDirection.y; moveDirection = (forward * curSpeedX) + (right * curSpeedY); if (Input.GetAxis("Horizontal") < 0) { animator.SetBool("walkingLeft", true); } else { animator.SetBool("walkingLeft", false); } if (Input.GetAxis("Horizontal") > 0) { animator.SetBool("walkingRight", true); } else { animator.SetBool("walkingRight", false); } if (Input.GetAxis("Vertical") > 0) { animator.SetBool("walkingForward", true); } else { animator.SetBool("walkingForward", false); } if (Input.GetAxis("Vertical") < 0) { animator.SetBool("walkingBack", true); } else { animator.SetBool("walkingBack", false); } if (isRunning) { Debug.Log("running"); } if (Input.GetButtonDown("Jump") && canMove && characterController.isGrounded) { moveDirection.y = jumpSpeed; animator.SetTrigger("Jump"); } else { moveDirection.y = movementDirectionY; } // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied // as an acceleration (ms^-2) if (!characterController.isGrounded) { moveDirection.y -= gravity * Time.deltaTime; } // Move the controller characterController.Move(moveDirection * Time.deltaTime); // Player and Camera rotation if (canMove) { rotationX += -Input.GetAxis("Mouse Y") * lookSpeed; rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit); playerCamera.transform.localRotation = Quaternion.Euler(rotationX, rotationY, 0); transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0); } } } public void SetPosition() { transform.position=new Vector3(Random.RandomRange(-5,5),0.8f,Random.Range(-15,7)); } }
Editor is loading...