75 lines
1.9 KiB
C#
75 lines
1.9 KiB
C#
![]() |
using UnityEngine;
|
||
|
|
||
|
public class ChasePlayerController : MonoBehaviour
|
||
|
{
|
||
|
public float moveSpeed = 5f;
|
||
|
public float laneDistance = 2.5f; // Distance between lanes
|
||
|
public float jumpForce = 7f;
|
||
|
public float laneSwitchSpeed = 10f;
|
||
|
|
||
|
private int currentLane = 1; // 0 = Left, 1 = Middle, 2 = Right
|
||
|
private Rigidbody rb;
|
||
|
private Animator animator;
|
||
|
private bool isGrounded = true;
|
||
|
|
||
|
private Vector3 targetPosition;
|
||
|
|
||
|
void Start()
|
||
|
{
|
||
|
rb = GetComponent<Rigidbody>();
|
||
|
animator = GetComponent<Animator>();
|
||
|
targetPosition = transform.position;
|
||
|
}
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
HandleInput();
|
||
|
HandleLaneSwitch();
|
||
|
MoveForward();
|
||
|
}
|
||
|
|
||
|
void HandleInput()
|
||
|
{
|
||
|
if (Input.GetKeyDown(KeyCode.LeftArrow))
|
||
|
{
|
||
|
if (currentLane > 0)
|
||
|
currentLane--;
|
||
|
}
|
||
|
|
||
|
if (Input.GetKeyDown(KeyCode.RightArrow))
|
||
|
{
|
||
|
if (currentLane < 2)
|
||
|
currentLane++;
|
||
|
}
|
||
|
|
||
|
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
|
||
|
{
|
||
|
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
|
||
|
isGrounded = false;
|
||
|
if (animator != null) animator.SetTrigger("Jump");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void HandleLaneSwitch()
|
||
|
{
|
||
|
float targetX = (currentLane - 1) * laneDistance;
|
||
|
targetPosition = new Vector3(targetX, transform.position.y, transform.position.z);
|
||
|
transform.position = Vector3.Lerp(transform.position, targetPosition, laneSwitchSpeed * Time.deltaTime);
|
||
|
}
|
||
|
|
||
|
void MoveForward()
|
||
|
{
|
||
|
transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
|
||
|
}
|
||
|
|
||
|
void OnCollisionEnter(Collision collision)
|
||
|
{
|
||
|
if (collision.gameObject.CompareTag("Ground"))
|
||
|
{
|
||
|
Debug.Log("Ground");
|
||
|
isGrounded = true;
|
||
|
if (animator != null) animator.SetTrigger("Run");
|
||
|
}
|
||
|
}
|
||
|
}
|