144 lines
4.1 KiB
C#
144 lines
4.1 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;
|
|
|
|
private Vector2 startTouchPosition;
|
|
private Vector2 endTouchPosition;
|
|
private bool swipeDetected = false;
|
|
private float minSwipeDistance = 50f; // Pixels
|
|
|
|
void Start()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
animator = GetComponent<Animator>();
|
|
targetPosition = transform.position;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
HandleInput();
|
|
HandleSwipe(); // Add this
|
|
HandleLaneSwitch();
|
|
MoveForward();
|
|
}
|
|
|
|
void HandleInput()
|
|
{
|
|
// Keyboard controls (for testing in editor)
|
|
if (Input.GetKeyDown(KeyCode.LeftArrow))
|
|
{
|
|
if (currentLane > 0)
|
|
currentLane--;
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.RightArrow))
|
|
{
|
|
if (currentLane < 2)
|
|
currentLane++;
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
|
|
{
|
|
Jump();
|
|
}
|
|
}
|
|
|
|
void HandleSwipe()
|
|
{
|
|
if (Input.touchCount == 1)
|
|
{
|
|
Touch touch = Input.GetTouch(0);
|
|
|
|
switch (touch.phase)
|
|
{
|
|
case TouchPhase.Began:
|
|
startTouchPosition = touch.position;
|
|
swipeDetected = true;
|
|
break;
|
|
|
|
case TouchPhase.Ended:
|
|
if (swipeDetected)
|
|
{
|
|
endTouchPosition = touch.position;
|
|
Vector2 swipe = endTouchPosition - startTouchPosition;
|
|
|
|
if (swipe.magnitude >= minSwipeDistance)
|
|
{
|
|
float x = swipe.x;
|
|
float y = swipe.y;
|
|
|
|
if (Mathf.Abs(x) > Mathf.Abs(y))
|
|
{
|
|
// Horizontal swipe
|
|
if (x > 0)
|
|
{
|
|
// Swipe Right
|
|
if (currentLane < 2)
|
|
currentLane++;
|
|
}
|
|
else
|
|
{
|
|
// Swipe Left
|
|
if (currentLane > 0)
|
|
currentLane--;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Vertical swipe
|
|
if (y > 0 && isGrounded)
|
|
{
|
|
// Swipe Up = Jump
|
|
Jump();
|
|
}
|
|
}
|
|
}
|
|
|
|
swipeDetected = false;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Jump()
|
|
{
|
|
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"))
|
|
{
|
|
isGrounded = true;
|
|
if (animator != null) animator.SetTrigger("Run");
|
|
}
|
|
}
|
|
}
|