Untitled
unknown
plain_text
7 months ago
1.1 kB
5
Indexable
using System.Collections;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public CharacterController controller;
public float speed = 5f;
public float gravity = -9.8f;
public float jumpHeight = 1.5f;
public Transform groundCheck;
public LayerMask groundMask;
private Vector3 velocity;
private bool isGrounded;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, 0.3f, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Editor is loading...
Leave a Comment