Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(Camera))]
- public class GhostFreeRoamCamera : MonoBehaviour
- {
- public float speed = 10.0f;
- public float sprintSpeed = 25.0f;
- public float jumpSpeed = 8.0f;
- public float gravity = 20.0f;
- public float increaseSpeed = 5f;
- public float damage = 10f;
- public float range = 100f;
- public bool allowMovement = true;
- public bool allowRotation = true;
- public KeyCode forwardButton = KeyCode.W;
- public KeyCode backwardButton = KeyCode.S;
- public KeyCode rightButton = KeyCode.D;
- public KeyCode leftButton = KeyCode.A;
- public KeyCode jumpButton = KeyCode.Space;
- public KeyCode sprintButton = KeyCode.LeftShift;
- public float cursorSensitivity = 0.025f;
- public bool cursorToggleAllowed = true;
- public KeyCode cursorToggleButton = KeyCode.Escape;
- private Vector3 moveDirection = Vector3.zero;
- public ParticleSystem muzzleFlash;
- public GameObject impactEffect;
- private float currentSpeedHorizontal = 0f;
- private float currentSpeedVertical = 0f;
- private bool moving = false;
- private bool togglePressed = false;
- private CharacterController controller;
- public Camera fpsCam;
- private void Start() {
- controller = GetComponent<CharacterController>();
- }
- private void OnEnable()
- {
- if (cursorToggleAllowed)
- {
- Screen.lockCursor = true;
- Cursor.visible = false;
- }
- }
- private void Update()
- {
- if (controller.isGrounded) {
- moveDirection = new Vector3(0.0f, 0.0f, 0.0f);
- CheckMove(forwardButton, ref moveDirection, transform.forward);
- CheckMove(backwardButton, ref moveDirection, -transform.forward);
- CheckMove(rightButton, ref moveDirection, transform.right);
- CheckMove(leftButton, ref moveDirection, -transform.right);
- if (Input.GetKey(sprintButton)) {
- moveDirection *= sprintSpeed;
- }
- else {
- moveDirection *= speed;
- }
- if (Input.GetKey(jumpButton)) {
- moveDirection.y = jumpSpeed;
- }
- }
- moveDirection.y -= gravity * Time.deltaTime;
- controller.Move(moveDirection * Time.deltaTime);
- if (Input.GetButtonDown("Fire1"))
- Shoot();
- if (allowRotation)
- {
- Vector3 eulerAngles = transform.eulerAngles;
- eulerAngles.x += -Input.GetAxis("Mouse Y") * 359f * cursorSensitivity;
- eulerAngles.y += Input.GetAxis("Mouse X") * 359f * cursorSensitivity;
- transform.eulerAngles = eulerAngles;
- }
- if (cursorToggleAllowed)
- {
- if (Input.GetKey(cursorToggleButton))
- {
- if (!togglePressed)
- {
- togglePressed = true;
- Screen.lockCursor = !Screen.lockCursor;
- Cursor.visible = !Cursor.visible;
- }
- }
- else togglePressed = false;
- }
- else
- {
- togglePressed = false;
- Cursor.visible = false;
- }
- }
- private void CheckMove(KeyCode keyCode, ref Vector3 deltaPosition, Vector3 directionVector)
- {
- if (Input.GetKey(keyCode))
- {
- moving = true;
- deltaPosition += directionVector;
- }
- }
- private void Shoot() {
- muzzleFlash.Play();
- RaycastHit hit;
- if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
- {
- }
- GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
- Destroy(impactGO, 2f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement