You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.9 KiB

using System;
using System.Collections;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using Random = System.Random;
public class PlayerMove : MonoBehaviour
{
[SerializeField] private float basespeed;
[SerializeField] private float speed;
[SerializeField] private float speedincrease;
[SerializeField] private float maxspeed;
private Rigidbody rb;
[SerializeField] private float slide;
Vector3 moveDirection;
bool vertical;
private bool horizontal;
[SerializeField] StandUpCollider standUp;
[SerializeField] private string inputNameHorizontal;
[SerializeField] private string inputNameVertical;
public Transform player;
public Slider awarenessSlider;
private bool walking = true;
private Quaternion lastRotation;
private void Start()
{
rb = GetComponent<Rigidbody>();
standUp = GetComponentInChildren<StandUpCollider>();
StartCoroutine(MakeSound(7f));
}
void Update()
{
float horizontalInput = Input.GetAxis(inputNameHorizontal);
float verticalInput = Input.GetAxis(inputNameVertical);
horizontalInput = Mathf.Round(horizontalInput);
verticalInput = Mathf.Round(verticalInput);
if (horizontalInput != 0 && horizontal == true)
{
if (!standUp.ispushing && horizontalInput == 1)
{
player.rotation = Quaternion.Euler(90f, 90f, 0f);
lastRotation = Quaternion.Euler(0f, 90f, 0f);
}
else if (!standUp.ispushing && horizontalInput == -1)
{
player.rotation = Quaternion.Euler(90f, 260f, 0f);
lastRotation = Quaternion.Euler(0f, 260f, 0f);
}
else if (horizontalInput == 1)
{
player.rotation = Quaternion.Euler(0f, 90f, 0f);
}
else if (horizontalInput == -1)
{
player.rotation = Quaternion.Euler(0f, 260f, 0f);
}
vertical = false;
moveDirection = new Vector3(horizontalInput,0, 0);
moveDirection.Normalize();
if (!vertical && speed <= maxspeed && !standUp.ispushing)
{
speed += speedincrease;
}
}
else if (verticalInput != 0 && vertical == true)
{
if (!standUp.ispushing && verticalInput == 1)
{
player.rotation = Quaternion.Euler(90f, 0f, 0f);
lastRotation = Quaternion.Euler(0f, 0f, 0f);
}
else if (!standUp.ispushing && verticalInput == -1)
{
player.rotation = Quaternion.Euler(90f, 180f, 0f);
lastRotation = Quaternion.Euler(0f, 180f, 0f);
}
else if (verticalInput == 1)
{
player.rotation = Quaternion.Euler(0f, 0f, 0f);
}
else if (verticalInput == -1)
{
player.rotation = Quaternion.Euler(0f, 180f, 0f);
}
horizontal = false;
moveDirection = new Vector3(0, 0, verticalInput);
moveDirection.Normalize();
if (!horizontal && speed <= maxspeed && !standUp.ispushing)
{
speed += speedincrease;
}
}
else
{
StartCoroutine(Slide(slide));
if (!standUp.ispushing)
{
player.rotation = lastRotation;
}
}
transform.Translate(moveDirection * speed * Time.deltaTime, Space.World);
if (moveDirection != Vector3.zero && walking)
{
StartCoroutine(Step(0.3f));
}
}
IEnumerator Slide(float delay)
{
yield return new WaitForSeconds(delay);
moveDirection = new Vector3(0, 0, 0);
horizontal = true;
vertical = true;
speed = basespeed;
}
IEnumerator Step(float delay)
{
walking = false;
yield return new WaitForSeconds(delay);
awarenessSlider.value += 1f;
walking = true;
}
IEnumerator MakeSound(float delay)
{
while (true)
{
yield return new WaitForSeconds(delay);
SoundEffectManager.Play("Penguin Sound");
}
}
}