56 lines
1.4 KiB
C#
56 lines
1.4 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerMovement : MonoBehaviour
|
|
{
|
|
public CharacterController controller;
|
|
|
|
public Transform modelTransform;
|
|
|
|
public Transform foamGeneratorParent;
|
|
|
|
public float rotationSpeed = 5f;
|
|
|
|
public float speed = 12f;
|
|
|
|
public float gravity = -10f;
|
|
|
|
public float jumpHeight = 2f;
|
|
|
|
public Transform groundCheck;
|
|
|
|
public float groundDistance = 0.4f;
|
|
|
|
public LayerMask groundMask;
|
|
|
|
private Vector3 velocity;
|
|
|
|
private bool isGrounded;
|
|
|
|
private void Update()
|
|
{
|
|
float num = 0f - Input.GetAxis("Horizontal");
|
|
float num2 = 0f - Input.GetAxis("Vertical");
|
|
bool buttonDown = Input.GetButtonDown("Jump");
|
|
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
|
|
if (isGrounded && velocity.y < 0f)
|
|
{
|
|
velocity.y = -2f;
|
|
}
|
|
Vector3 value = base.transform.right * num + base.transform.forward * num2;
|
|
value = Vector3.Normalize(value);
|
|
controller.Move(value * speed * Time.deltaTime);
|
|
if (buttonDown && isGrounded)
|
|
{
|
|
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
|
}
|
|
velocity.y += gravity * Time.deltaTime;
|
|
controller.Move(velocity * Time.deltaTime);
|
|
if (value != Vector3.zero)
|
|
{
|
|
Quaternion to = Quaternion.LookRotation(value, Vector3.up);
|
|
modelTransform.rotation = Quaternion.RotateTowards(modelTransform.rotation, to, rotationSpeed * Time.deltaTime);
|
|
}
|
|
foamGeneratorParent.localScale = Vector3.one * value.magnitude;
|
|
}
|
|
}
|