Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- //References to other Game objects
- Camera cam;
- Rigidbody myRigidbody;
- //Editor exposed variables for easy balancing
- [SerializeField] private float speed = 10.0f;
- [SerializeField] private float strafeSpeed = 3.0f;
- [SerializeField] private float rotationSpeed = 5.0f;
- [SerializeField] private float maxFOV = 45.0f;
- [SerializeField] private LayerMask whatIsInteractable;
- float itemRotX, itemRotY;
- Transform viewItemLocation;
- GameObject currentItem;
- bool isHoldingItem = false;
- [SerializeField] List<GameObject> inventory = new List<GameObject>();
- // working variables for player movement
- float forwardMovement, sideMovement, rotationDeltaY, rotationDeltaX, rotX, rotY;
- // Start is called before the first frame update
- void Start()
- {
- myRigidbody = GetComponent<Rigidbody>();
- cam = GetComponentInChildren<Camera>();
- viewItemLocation = GameObject.Find("View Item Location").transform;
- Cursor.lockState = CursorLockMode.Locked;
- }
- // Update is called once per frame
- void Update()
- {
- //Load all of our inputs into float variables
- forwardMovement = Input.GetAxis("Vertical");
- sideMovement = Input.GetAxis("Horizontal");
- rotationDeltaY = Input.GetAxis("Mouse Y");
- rotationDeltaX = Input.GetAxis("Mouse X");
- if (isHoldingItem == true)
- {
- currentItem.transform.position = viewItemLocation.position;
- }
- else
- // Add up our rotation delta
- rotX = rotX + (rotationDeltaX * rotationSpeed);
- rotY = rotY + rotationDeltaY;
- rotY = Mathf.Clamp(rotY, -maxFOV, maxFOV);
- //Apply movement values to our player
- myRigidbody.velocity = (transform.forward * forwardMovement * speed) + (transform.right * sideMovement * strafeSpeed);
- //Apply rotation values to our player and camera
- transform.localRotation = Quaternion.Euler(new Vector3(0.0f, rotX, 0.0f));
- cam.transform.localRotation = Quaternion.Euler(new Vector3(-rotY, 0.0f, 0.0f));
- Ray eyeLine = new Ray(cam.transform.position, cam.transform.forward);
- RaycastHit item;
- if (Physics.Raycast(eyeLine, out item, 45.0f, whatIsInteractable))
- {
- if (Input.GetMouseButton(0))
- {
- isHoldingItem = true;
- currentItem = item.transform.gameObject;
- currentItem.GetComponent<Rigidbody>().velocity = new Vector3(0.0f, 0.0f, 0.0f);
- }
- }
- if (isHoldingItem == true)
- {
- Debug.Log("CI = " + currentItem);
- Debug.Log("VIL = " + viewItemLocation);
- currentItem.transform.position = viewItemLocation.position;
- itemRotX = itemRotX + rotationDeltaX;
- itemRotY = itemRotY + rotationDeltaY;
- currentItem.transform.localRotation = Quaternion.Euler(itemRotY, itemRotX, 0.0f);
- }
- if (Input.GetMouseButtonDown(0))
- {
- Inventory.Instance.AddItem(currentItem.GetComponent<ItemObject>().name);
- Destroy(currentItem.gameObject);
- Debug.Log("Added item to inventory");
- inventory.Add(currentItem);
- currentItem.SetActive(false);
- itemRotX = 0.0f;
- itemRotY = 0.0f;
- currentItem = null;
- isHoldingItem = false;
- }
- if (Input.GetMouseButton(1))
- {
- currentItem.GetComponent<Rigidbody>().velocity = new Vector3(0.0f, 0.0f, 0.0f);
- itemRotX = 0.0f;
- itemRotY = 0.0f;
- currentItem = null;
- isHoldingItem = false;
- }
- if (Input.GetKeyDown(KeyCode.Alpha1))
- {
- if (Inventory.Instance.GetInventorySize()>= 1)
- {
- isHoldingItem = true;
- currentItem = Instantiate(Inventory.Instance.GetItem(0));
- currentItem.GetComponent<Rigidbody>().velocity = new Vector3(0.0f, 0.0f, 0.0f);
- }
- }
- if (Input.GetKeyDown(KeyCode.Alpha2))
- {
- if (Inventory.Instance.GetInventorySize() >= 2)
- {
- isHoldingItem = true;
- currentItem = Instantiate(Inventory.Instance.GetItem(1));
- currentItem.GetComponent<Rigidbody>().velocity = new Vector3(0.0f, 0.0f, 0.0f);
- }
- }
- if (Input.GetKeyDown(KeyCode.Alpha3))
- {
- if (Inventory.Instance.GetInventorySize() >= 3)
- {
- isHoldingItem = true;
- currentItem = Instantiate(Inventory.Instance.GetItem(2));
- currentItem.GetComponent<Rigidbody>().velocity = new Vector3(0.0f, 0.0f, 0.0f);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement