Initial commit
This commit is contained in:
79
Assets/Scripts/PlayerBergerak1.cs
Normal file
79
Assets/Scripts/PlayerBergerak1.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public class PlayerBergerak1 : MonoBehaviour
|
||||
{
|
||||
public float kecepatan = 5f;
|
||||
public float gravitasi = -9.81f;
|
||||
public Transform groundCheck;
|
||||
public float groundDistance = 0.4f;
|
||||
public LayerMask groundMask;
|
||||
public bool Gerak = true;
|
||||
|
||||
public Transform kameraVR;
|
||||
private Vector3 velocity;
|
||||
private bool isGrounded;
|
||||
private CharacterController controller;
|
||||
|
||||
void Start()
|
||||
{
|
||||
controller = GetComponent<CharacterController>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (!Gerak) return;
|
||||
// Apakah player menyentuh tanah
|
||||
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
|
||||
|
||||
if (isGrounded && velocity.y < 0)
|
||||
{
|
||||
velocity.y = -2f; //
|
||||
}
|
||||
|
||||
float x = Input.GetAxis("Horizontal");
|
||||
float z = Input.GetAxis("Vertical");
|
||||
|
||||
|
||||
// Ambil arah dari kamera (headset)
|
||||
Vector3 forward = kameraVR.forward;
|
||||
Vector3 right = kameraVR.right;
|
||||
|
||||
// Hapus komponen vertikal biar tidak naik/turun
|
||||
forward.y = 0f;
|
||||
right.y = 0f;
|
||||
|
||||
// Normalisasi vektor
|
||||
forward.Normalize();
|
||||
right.Normalize();
|
||||
|
||||
Vector3 move = (right * x + forward * z);
|
||||
|
||||
|
||||
// Horizontal Move
|
||||
controller.Move(move * kecepatan * Time.deltaTime);
|
||||
|
||||
// Gravitasi
|
||||
velocity.y += gravitasi * Time.deltaTime;
|
||||
|
||||
// Vertikal Move
|
||||
controller.Move(velocity * Time.deltaTime);
|
||||
}
|
||||
|
||||
public void TeleportKe(Vector3 posisiTarget)
|
||||
{
|
||||
CharacterController cc = GetComponent<CharacterController>();
|
||||
if (cc != null)
|
||||
{
|
||||
cc.enabled = false;
|
||||
|
||||
// Atur posisi player ke target (Y-nya dikasih offset dikit supaya nggak nyangkut)
|
||||
transform.position = posisiTarget + new Vector3(0, 0.1f, 0);
|
||||
|
||||
// Reset velocity biar ga jatuh lagi
|
||||
velocity.y = 0;
|
||||
|
||||
cc.enabled = true;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user