Advertisement
Guest User

Untitled

a guest
May 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Camera))]
  4. public class GhostFreeRoamCamera : MonoBehaviour
  5. {
  6.     public float speed = 10.0f;
  7.     public float sprintSpeed = 25.0f;
  8.     public float jumpSpeed = 8.0f;
  9.     public float gravity = 20.0f;
  10.     public float increaseSpeed = 5f;
  11.  
  12.     public float damage = 10f;
  13.     public float range = 100f;
  14.  
  15.     public bool allowMovement = true;
  16.     public bool allowRotation = true;
  17.  
  18.     public KeyCode forwardButton = KeyCode.W;
  19.     public KeyCode backwardButton = KeyCode.S;
  20.     public KeyCode rightButton = KeyCode.D;
  21.     public KeyCode leftButton = KeyCode.A;
  22.     public KeyCode jumpButton = KeyCode.Space;
  23.     public KeyCode sprintButton = KeyCode.LeftShift;
  24.  
  25.     public float cursorSensitivity = 0.025f;
  26.     public bool cursorToggleAllowed = true;
  27.     public KeyCode cursorToggleButton = KeyCode.Escape;
  28.  
  29.     private Vector3 moveDirection = Vector3.zero;
  30.  
  31.     public ParticleSystem muzzleFlash;
  32.     public GameObject impactEffect;
  33.  
  34.     private float currentSpeedHorizontal = 0f;
  35.     private float currentSpeedVertical = 0f;
  36.     private bool moving = false;
  37.     private bool togglePressed = false;
  38.  
  39.     private CharacterController controller;
  40.  
  41.     public Camera fpsCam;
  42.  
  43.     private void Start() {
  44.        controller = GetComponent<CharacterController>();
  45.     }
  46.    
  47.     private void OnEnable()
  48.     {
  49.         if (cursorToggleAllowed)
  50.         {
  51.             Screen.lockCursor = true;
  52.             Cursor.visible = false;
  53.         }
  54.     }
  55.  
  56.     private void Update()
  57.     {
  58.         if (controller.isGrounded) {
  59.           moveDirection = new Vector3(0.0f, 0.0f, 0.0f);
  60.           CheckMove(forwardButton, ref moveDirection, transform.forward);
  61.           CheckMove(backwardButton, ref moveDirection, -transform.forward);
  62.           CheckMove(rightButton, ref moveDirection, transform.right);
  63.           CheckMove(leftButton, ref moveDirection, -transform.right);          
  64.          
  65.           if (Input.GetKey(sprintButton)) {
  66.              moveDirection *= sprintSpeed;
  67.           }
  68.           else {
  69.              moveDirection *= speed;
  70.           }
  71.          
  72.           if (Input.GetKey(jumpButton)) {
  73.             moveDirection.y = jumpSpeed;
  74.           }
  75.        }
  76.        
  77.         moveDirection.y -= gravity * Time.deltaTime;
  78.        
  79.         controller.Move(moveDirection * Time.deltaTime);
  80.  
  81.         if (Input.GetButtonDown("Fire1"))
  82.             Shoot();
  83.        
  84.         if (allowRotation)
  85.         {
  86.             Vector3 eulerAngles = transform.eulerAngles;
  87.             eulerAngles.x += -Input.GetAxis("Mouse Y") * 359f * cursorSensitivity;
  88.             eulerAngles.y += Input.GetAxis("Mouse X") * 359f * cursorSensitivity;
  89.             transform.eulerAngles = eulerAngles;
  90.         }
  91.  
  92.         if (cursorToggleAllowed)
  93.         {
  94.             if (Input.GetKey(cursorToggleButton))
  95.             {
  96.                 if (!togglePressed)
  97.                 {
  98.                     togglePressed = true;
  99.                     Screen.lockCursor = !Screen.lockCursor;
  100.                     Cursor.visible = !Cursor.visible;
  101.                 }
  102.             }
  103.             else togglePressed = false;
  104.         }
  105.         else
  106.         {
  107.             togglePressed = false;
  108.             Cursor.visible = false;
  109.         }
  110.     }
  111.  
  112.     private void CheckMove(KeyCode keyCode, ref Vector3 deltaPosition, Vector3 directionVector)
  113.     {
  114.         if (Input.GetKey(keyCode))
  115.         {
  116.             moving = true;
  117.             deltaPosition += directionVector;
  118.         }
  119.     }
  120.    
  121.     private void Shoot() {
  122.         muzzleFlash.Play();
  123.         RaycastHit hit;
  124.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
  125.         {
  126.  
  127.         }
  128.  
  129.         GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
  130.         Destroy(impactGO, 2f);
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement