51 lines
1.3 KiB
C#
51 lines
1.3 KiB
C#
using UnityEngine;
|
|
public class PlayerMovement : MonoBehaviour
|
|
{
|
|
public CharacterController controller;
|
|
public float speed = 12f;
|
|
public float gravity = -10f;
|
|
public float jumpHeight = 2f;
|
|
public Transform groundCheck;
|
|
public float groundDistance = 0.4f;
|
|
public LayerMask groundMask;
|
|
Vector3 startPos;
|
|
Vector3 velocity;
|
|
bool isGrounded;
|
|
float x;
|
|
float z;
|
|
bool jumpPressed = false;
|
|
private void Start()
|
|
{
|
|
startPos = transform.localPosition;
|
|
}
|
|
void Update()
|
|
{
|
|
x = Input.GetAxis("Horizontal");
|
|
z = Input.GetAxis("Vertical");
|
|
jumpPressed = Input.GetButtonDown("Jump");
|
|
|
|
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
|
|
|
|
if (isGrounded && velocity.y < 0)
|
|
{
|
|
velocity.y = -2f;
|
|
}
|
|
|
|
Vector3 move = transform.right * x + transform.forward * z;
|
|
|
|
controller.Move(move * speed * Time.deltaTime);
|
|
|
|
if (jumpPressed && isGrounded)
|
|
{
|
|
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
|
}
|
|
velocity.y += gravity * Time.deltaTime;
|
|
Physics.autoSyncTransforms = true;
|
|
controller.Move(velocity * Time.deltaTime);
|
|
}
|
|
public void ResetPos()
|
|
{
|
|
transform.localPosition = startPos;
|
|
}
|
|
}
|