GlobalGameJam-2021/Assets/Scripts/PlayerManager.cs

87 lines
1.9 KiB
C#
Raw Normal View History

2021-01-29 19:55:53 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour
{
public float speed = 8;
public float runSpeed = 20;
public float rotateSpeed = 5;
public float mouseSensitive = 100;
2021-01-29 22:00:16 +00:00
public float jump = 5;
private Vector3 jumpPower = new Vector3();
public bool onGround = false;
2021-01-29 19:55:53 +00:00
private bool run = false;
private Camera playerCamera;
private Rigidbody rigidBody;
// Start is called before the first frame update
void Start()
{
playerCamera = GetComponentInChildren<Camera>();
rigidBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit(0);
}
2021-01-29 22:00:16 +00:00
Jump();
2021-01-29 19:55:53 +00:00
Move();
RunSwitch();
}
void RunSwitch()
{
if (Input.GetAxisRaw("Run") > 0)
{
run = true;
}
else
{
run = false;
}
}
void Move()
{
rigidBody.MovePosition(
transform.position +
2021-01-29 22:00:16 +00:00
(transform.right * (run ? runSpeed : speed) * Input.GetAxis("Horizontal") * Time.deltaTime) +
jumpPower
2021-01-29 19:55:53 +00:00
);
}
2021-01-29 22:00:16 +00:00
void Jump()
{
Debug.Log(rigidBody.velocity.y);
if (Input.GetAxisRaw("Jump") > 0) {
if (rigidBody.velocity.y <= 2 && onGround)
{
jumpPower = transform.up * jump * Time.deltaTime;
}
}
}
public void OnTriggerEnter(Collider other)
{
2021-01-29 22:56:23 +00:00
if (other.tag == "Ground" || other.tag == "Objects")
2021-01-29 22:00:16 +00:00
{
jumpPower = Vector3.zero;
onGround = true;
}
}
public void OnTriggerExit(Collider other)
{
2021-01-29 22:56:23 +00:00
if (other.tag == "Ground" || other.tag == "Objects")
2021-01-29 22:00:16 +00:00
{
onGround = false;
}
}
2021-01-29 19:55:53 +00:00
}