GlobalGameJam-2021/Assets/Scripts/PlayerManager.cs

122 lines
3.0 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
{
2021-01-30 09:51:42 +00:00
public float speed = 25;
public float runSpeed = 50;
2021-01-29 19:55:53 +00:00
public float mouseSensitive = 100;
2021-01-30 09:51:42 +00:00
public float jump = 30;
2021-01-29 22:00:16 +00:00
public bool onGround = false;
2021-01-29 19:55:53 +00:00
private bool run = false;
private Camera playerCamera;
private Rigidbody rigidBody;
2021-01-30 09:51:42 +00:00
public GameObject playerModel;
2021-01-29 19:55:53 +00:00
// 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);
}
Move();
RunSwitch();
2021-01-30 09:51:42 +00:00
Animation();
}
private void FixedUpdate()
{
Jump();
2021-01-29 19:55:53 +00:00
}
void RunSwitch()
{
if (Input.GetAxisRaw("Run") > 0)
{
run = true;
}
else
{
run = false;
}
}
2021-01-30 09:51:42 +00:00
void Animation()
{
float localSpeed = 5f;
if (Input.GetAxis("Horizontal") > 0)
{
playerModel.transform.rotation = Quaternion.Lerp(
playerModel.transform.rotation,
Quaternion.Euler(playerModel.transform.eulerAngles.x, 0f, playerModel.transform.eulerAngles.z),
localSpeed * Time.deltaTime
);
}
else if (Input.GetAxis("Horizontal") < 0)
{
playerModel.transform.rotation = Quaternion.Lerp(
playerModel.transform.rotation,
Quaternion.Euler(playerModel.transform.eulerAngles.x, -180f, playerModel.transform.eulerAngles.z),
localSpeed * Time.deltaTime
);
}
}
2021-01-29 19:55:53 +00:00
void Move()
{
rigidBody.MovePosition(
transform.position +
2021-01-30 09:51:42 +00:00
(transform.right * (run ? runSpeed : speed) * Input.GetAxis("Horizontal") * Time.deltaTime)
2021-01-29 19:55:53 +00:00
);
}
2021-01-29 22:00:16 +00:00
void Jump()
{
if (Input.GetAxisRaw("Jump") > 0) {
2021-01-30 09:51:42 +00:00
if (rigidBody.velocity.y <= 1 && onGround)
2021-01-29 22:00:16 +00:00
{
2021-01-30 09:51:42 +00:00
rigidBody.AddForce(transform.up * jump * 10 * Time.deltaTime, ForceMode.VelocityChange);
2021-01-29 22:00:16 +00:00
}
}
}
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
{
2021-01-30 09:51:42 +00:00
rigidBody.MovePosition(transform.position);
2021-01-29 22:00:16 +00:00
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-30 09:51:42 +00:00
public void OnTriggerStay(Collider other)
{
if (other.tag == "Ground" || other.tag == "Objects")
{
if (rigidBody.velocity.y <= 1 && !onGround)
{
rigidBody.MovePosition(transform.position);
onGround = true;
}
}
}
2021-01-29 19:55:53 +00:00
}