You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
C#
47 lines
1.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerBehavior : MonoBehaviour
|
|
{
|
|
|
|
private float moveSpeed = 7f;
|
|
private float mass = 1f;
|
|
CharacterController controller;
|
|
private float jumpSpeed = 7f;
|
|
|
|
Vector3 velocity;
|
|
|
|
// Start is called before the first frame update
|
|
private void Start()
|
|
{
|
|
controller = GetComponent<CharacterController>();
|
|
}
|
|
|
|
void Gravity(){
|
|
var gravity = Physics.gravity * mass * Time.deltaTime;
|
|
velocity.y = controller.isGrounded ? -1f : velocity.y + gravity.y;
|
|
}
|
|
|
|
void Move(){
|
|
float horizontal = Input.GetAxis("Horizontal");
|
|
float vertical = Input.GetAxis("Vertical");
|
|
|
|
Vector3 input = new Vector3();
|
|
input += transform.forward * vertical;
|
|
input += transform.right * horizontal;
|
|
input = Vector3.ClampMagnitude(input, 1f);
|
|
|
|
if(Input.GetKey(KeyCode.Space) && controller.isGrounded){
|
|
velocity.y += jumpSpeed;
|
|
}
|
|
|
|
controller.Move((input * moveSpeed + velocity) * Time.deltaTime);
|
|
}
|
|
|
|
void Update(){
|
|
Gravity();
|
|
Move();
|
|
}
|
|
}
|