104 lines
2.8 KiB
C#
104 lines
2.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.Burst.Intrinsics;
|
|
using UnityEditor.Experimental.GraphView;
|
|
using UnityEngine;
|
|
using UnityEngine.Analytics;
|
|
using UnityEngine.Animations;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class Controls : MonoBehaviour
|
|
{
|
|
public float MOVEMENTSPEED = 10f;
|
|
public float SENSITIVITY = 10f;
|
|
Vector2 movement;
|
|
Vector2 rotation;
|
|
bool isAirborne;
|
|
bool isJumping;
|
|
Vector3 lastDirection;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
movement = new Vector2();
|
|
rotation = new Vector2();
|
|
isAirborne = true;
|
|
isJumping = false;
|
|
lastDirection = new Vector3();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
void FixedUpdate(){
|
|
applyInput();
|
|
if(Cursor.lockState == CursorLockMode.Locked){
|
|
GameObject.Find("Crosshair").transform.localScale = new Vector3(1,1,1);
|
|
}
|
|
else{
|
|
GameObject.Find("Crosshair").transform.localScale = new Vector3(0,0,0);
|
|
}
|
|
|
|
}
|
|
|
|
private void applyInput(){
|
|
//Do Cam manipulation according to input even if in air
|
|
Vector3 camManipulation = new Vector3(-rotation.y,0,0);
|
|
GameObject.Find("MainCamera").transform.Rotate(camManipulation * Time.deltaTime * SENSITIVITY);
|
|
|
|
//Directly rotate Player object so that movement works
|
|
Vector3 lookDirection = new Vector3(0, rotation.x, 0) * Time.deltaTime * SENSITIVITY;
|
|
gameObject.transform.Rotate(lookDirection);
|
|
|
|
if(isAirborne){
|
|
gameObject.transform.Translate(lastDirection);
|
|
}
|
|
else{
|
|
Vector3 direction = new Vector3(movement.x, isJumping ? 0.5f : 0, movement.y) * Time.deltaTime * MOVEMENTSPEED;
|
|
gameObject.transform.Translate(direction);
|
|
lastDirection = direction;
|
|
}
|
|
}
|
|
|
|
void OnMove(InputValue direction){
|
|
movement = direction.Get<Vector2>();
|
|
}
|
|
|
|
void OnLook(InputValue direction){
|
|
rotation = direction.Get<Vector2>();
|
|
}
|
|
|
|
void OnFire(){
|
|
Debug.Log("Fired");
|
|
if(Cursor.lockState == CursorLockMode.Locked){
|
|
Cursor.lockState = CursorLockMode.None;
|
|
}
|
|
else{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
}
|
|
}
|
|
|
|
void OnCollisionEnter(Collision col){
|
|
if(!isAirborne) return;
|
|
if(col.gameObject.name.ToLower().Contains("tile")){
|
|
isAirborne = false;
|
|
isJumping = false;
|
|
}
|
|
}
|
|
|
|
void OnCollisionExit(Collision col){
|
|
if(isAirborne) return;
|
|
if(col.gameObject.name.ToLower().Contains("tile")){
|
|
isAirborne = true;
|
|
}
|
|
}
|
|
|
|
void OnJump(){
|
|
if(isAirborne) return;
|
|
isJumping = true;
|
|
}
|
|
}
|