SHARE
TWEET

Untitled

a guest Jan 22nd, 2020 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     //References to other Game objects
  8.     Camera cam;
  9.     Rigidbody myRigidbody;
  10.  
  11.     //Editor exposed variables for easy balancing
  12.     [SerializeField] private float speed = 10.0f;
  13.     [SerializeField] private float strafeSpeed = 3.0f;
  14.     [SerializeField] private float rotationSpeed = 5.0f;
  15.     [SerializeField] private float maxFOV = 45.0f;
  16.  
  17.    
  18.  
  19.     [SerializeField] private LayerMask whatIsInteractable;
  20.  
  21.     float itemRotX, itemRotY;
  22.     Transform viewItemLocation;
  23.     GameObject currentItem;
  24.     bool isHoldingItem = false;
  25.  
  26.    
  27.  
  28.     [SerializeField] List<GameObject> inventory = new List<GameObject>();
  29.  
  30.      
  31.  
  32.     // working variables for player movement
  33.     float forwardMovement, sideMovement, rotationDeltaY, rotationDeltaX, rotX, rotY;
  34.  
  35.    
  36.  
  37.     // Start is called before the first frame update
  38.     void Start()
  39.     {
  40.         myRigidbody = GetComponent<Rigidbody>();
  41.         cam = GetComponentInChildren<Camera>();
  42.         viewItemLocation = GameObject.Find("View Item Location").transform;
  43.  
  44.         Cursor.lockState = CursorLockMode.Locked;
  45.     }
  46.  
  47.     // Update is called once per frame
  48.     void Update()
  49.     {
  50.         //Load all of our inputs into float variables
  51.         forwardMovement = Input.GetAxis("Vertical");
  52.         sideMovement = Input.GetAxis("Horizontal");
  53.         rotationDeltaY = Input.GetAxis("Mouse Y");
  54.         rotationDeltaX = Input.GetAxis("Mouse X");
  55.  
  56.         if (isHoldingItem == true)
  57.         {
  58.             currentItem.transform.position = viewItemLocation.position;
  59.         }
  60.  
  61.         else
  62.          
  63.         // Add up our rotation delta
  64.         rotX = rotX + (rotationDeltaX * rotationSpeed);
  65.  
  66.         rotY = rotY + rotationDeltaY;
  67.         rotY = Mathf.Clamp(rotY, -maxFOV, maxFOV);
  68.  
  69.         //Apply movement values to our player
  70.         myRigidbody.velocity = (transform.forward * forwardMovement * speed) + (transform.right * sideMovement * strafeSpeed);
  71.  
  72.  
  73.         //Apply rotation values to our player and camera
  74.         transform.localRotation = Quaternion.Euler(new Vector3(0.0f, rotX, 0.0f));
  75.         cam.transform.localRotation = Quaternion.Euler(new Vector3(-rotY, 0.0f, 0.0f));
  76.  
  77.         Ray eyeLine = new Ray(cam.transform.position, cam.transform.forward);
  78.         RaycastHit item;
  79.  
  80.         if (Physics.Raycast(eyeLine, out item, 45.0f, whatIsInteractable))
  81.         {
  82.             if (Input.GetMouseButton(0))
  83.             {
  84.                 isHoldingItem = true;
  85.                 currentItem = item.transform.gameObject;
  86.                 currentItem.GetComponent<Rigidbody>().velocity = new Vector3(0.0f, 0.0f, 0.0f);
  87.             }
  88.         }
  89.    
  90.            
  91.  
  92.  
  93.         if (isHoldingItem == true)
  94.         {
  95.             Debug.Log("CI = " + currentItem);
  96.             Debug.Log("VIL = " + viewItemLocation);
  97.  
  98.             currentItem.transform.position = viewItemLocation.position;
  99.  
  100.             itemRotX = itemRotX + rotationDeltaX;
  101.             itemRotY = itemRotY + rotationDeltaY;
  102.  
  103.             currentItem.transform.localRotation = Quaternion.Euler(itemRotY, itemRotX, 0.0f);
  104.         }
  105.  
  106.         if (Input.GetMouseButtonDown(0))
  107.         {
  108.             Inventory.Instance.AddItem(currentItem.GetComponent<ItemObject>().name);
  109.             Destroy(currentItem.gameObject);
  110.  
  111.             Debug.Log("Added item to inventory");
  112.             inventory.Add(currentItem);
  113.             currentItem.SetActive(false);
  114.  
  115.             itemRotX = 0.0f;
  116.             itemRotY = 0.0f;
  117.             currentItem = null;
  118.             isHoldingItem = false;
  119.         }
  120.  
  121.         if (Input.GetMouseButton(1))
  122.         {
  123.             currentItem.GetComponent<Rigidbody>().velocity = new Vector3(0.0f, 0.0f, 0.0f);
  124.             itemRotX = 0.0f;
  125.             itemRotY = 0.0f;
  126.             currentItem = null;
  127.             isHoldingItem = false;
  128.  
  129.         }
  130.  
  131.         if (Input.GetKeyDown(KeyCode.Alpha1))
  132.         {
  133.             if (Inventory.Instance.GetInventorySize()>= 1)
  134.             {
  135.                 isHoldingItem = true;
  136.                 currentItem = Instantiate(Inventory.Instance.GetItem(0));
  137.                 currentItem.GetComponent<Rigidbody>().velocity = new Vector3(0.0f, 0.0f, 0.0f);
  138.             }
  139.         }
  140.  
  141.         if (Input.GetKeyDown(KeyCode.Alpha2))
  142.         {
  143.             if (Inventory.Instance.GetInventorySize() >= 2)
  144.             {
  145.                 isHoldingItem = true;
  146.                 currentItem = Instantiate(Inventory.Instance.GetItem(1));
  147.                 currentItem.GetComponent<Rigidbody>().velocity = new Vector3(0.0f, 0.0f, 0.0f);
  148.             }
  149.         }
  150.  
  151.         if (Input.GetKeyDown(KeyCode.Alpha3))
  152.         {
  153.             if (Inventory.Instance.GetInventorySize() >= 3)
  154.             {
  155.                 isHoldingItem = true;
  156.                 currentItem = Instantiate(Inventory.Instance.GetItem(2));
  157.                 currentItem.GetComponent<Rigidbody>().velocity = new Vector3(0.0f, 0.0f, 0.0f);
  158.             }
  159.         }
  160.  
  161.  
  162.     }
  163.  
  164.  
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top