Kod-
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
CharacterController controller;
Vector3 velocity;
bool isGrounded;
public Transform ground;
public float distance = 0.3f;
public float speed;
public float jumpHeight;
public float gravity;
public LayerMask mask;
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void update()
{
#region Movement
float horizontal = Input.GetAxis("horizontal");
float vertical = Input.GetAxis("vertical");
Vector3 move = transform.right * horizontal * transform.forward * vertical;
controller.Move(move * speed * Time.deltaTime);
#endregion
#region Jump
if(Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
velocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravity);
}
#endregion
#region Gravity
isGrounded = Physics.CheckSphere(ground.position, distance, mask);
if(isGrounded && velocity.y <0)
{
velocity.y = 0f;
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
#endregion
}
}