You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.1 KiB
52 lines
1.1 KiB
2 months ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
|
||
|
public class FPController : MonoBehaviour
|
||
|
{
|
||
|
float speed = 0.1f;
|
||
|
Rigidbody rb;
|
||
|
CapsuleCollider capsule;
|
||
|
|
||
|
// Start is called before the first frame update
|
||
|
void Start()
|
||
|
{
|
||
|
rb = this.GetComponent<Rigidbody>();
|
||
|
capsule = this.GetComponent<CapsuleCollider>();
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
void Update()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
void FixedUpdate()
|
||
|
{
|
||
|
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
|
||
|
{
|
||
|
rb.AddForce(0, 300, 0);
|
||
|
}
|
||
|
|
||
|
float x = Input.GetAxis("Horizontal");
|
||
|
float z = Input.GetAxis("Vertical");
|
||
|
|
||
|
transform.position += new Vector3(x * speed, 0, z * speed);
|
||
|
}
|
||
|
|
||
|
bool IsGrounded()
|
||
|
{
|
||
|
RaycastHit hitInfo;
|
||
|
if(Physics.SphereCast(
|
||
|
transform.position,
|
||
|
capsule.radius,
|
||
|
Vector3.down,
|
||
|
out hitInfo,
|
||
|
(capsule.height / 2f) - capsule.radius + 0.1f))
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|