using System.Collections;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
    [Header("移动参数")]
    public float forwardSpeed = 5f;        // 前进速度
    public float laneChangeSpeed = 8f;     // 左右移动速度
    public float laneDistance = 2f;        // 跑道间距

    [Header("加速参数")]
    public float boostSpeed = 10f;         // 加速时的速度
    public float boostDuration = 5f;       // 加速持续时间

    private int currentLane = 1;           // 当前跑道 (0:左, 1:中, 2:右)
    private Vector3 targetPosition;        // 目标位置
    private bool isBoosting = false;       // 是否正在加速
    private float currentSpeed;            // 当前速度

    void Start()
    {
        currentSpeed = forwardSpeed;
        targetPosition = transform.position;
    }

    void Update()
    {
        // 处理左右移动输入
        HandleLaneChange();

        // 处理加速输入
        HandleBoost();

        // 移动玩家
        MovePlayer();
    }

    void HandleLaneChange()
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow) && currentLane > 0)
        {
            currentLane--;
            UpdateTargetPosition();
        }
        else if (Input.GetKeyDown(KeyCode.RightArrow) && currentLane < 2)
        {
            currentLane++;
            UpdateTargetPosition();
        }
    }

    void HandleBoost()
    {
        if (Input.GetKeyDown(KeyCode.R) && !isBoosting)
        {
            StartCoroutine(BoostCoroutine());
        }
    }

    void UpdateTargetPosition()
    {
        float xPos = (currentLane - 1) * laneDistance;
        targetPosition = new Vector3(xPos, transform.position.y, transform.position.z);
    }

    void MovePlayer()
    {
        // 向前移动
        Vector3 forwardMovement = Vector3.forward * currentSpeed * Time.deltaTime;
        transform.Translate(forwardMovement);

        // 左右移动
        Vector3 horizontalMovement = Vector3.zero;
        if (Vector3.Distance(transform.position, targetPosition) > 0.1f)
        {
            horizontalMovement = (targetPosition - transform.position).normalized * 
                                laneChangeSpeed * Time.deltaTime;
            horizontalMovement.y = 0;
            horizontalMovement.z = 0;
            transform.Translate(horizontalMovement);
        }
    }

    IEnumerator BoostCoroutine()
    {
        isBoosting = true;
        currentSpeed = boostSpeed;

        yield return new WaitForSeconds(boostDuration);

        currentSpeed = forwardSpeed;
        isBoosting = false;
    }

    // 获取玩家Z轴位置,用于跑道生成判断
    public float GetPlayerZPosition()
    {
        return transform.position.z;
    }
}

using System.Collections.Generic;
using UnityEngine;
public class RoadManager : MonoBehaviour
{
    [Header("跑道预制体")]
    public GameObject[] roadPrefabs;           // 四种跑道预制体
    public int initialRoadCount = 4;           // 初始跑道数量

    [Header("生成参数")]
    public float roadLength = 10f;             // 跑道长度
    public float spawnOffset = 20f;            // 生成偏移量
    public float spawnCheckDistance = 5f;      // 生成检查距离

    private Queue<GameObject> roadQueue = new Queue<GameObject>();
    private float nextSpawnZ = 0f;             // 下一个生成位置
    private Transform player;
    private bool hasSpawnedNext = false;       // 是否已生成下一个跑道

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;

        // 生成初始跑道
        for (int i = 0; i < initialRoadCount; i++)
        {
            SpawnRoad();
        }
    }

    void Update()
    {
        // 检查是否需要生成新跑道
        if (!hasSpawnedNext && player.position.z > nextSpawnZ - spawnCheckDistance)
        {
            SpawnRoad();
            hasSpawnedNext = true;
        }

        // 重置生成标志
        if (player.position.z > nextSpawnZ)
        {
            hasSpawnedNext = false;
        }
    }

    void SpawnRoad()
    {
        // 随机选择跑道预制体
        int randomIndex = Random.Range(0, roadPrefabs.Length);
        GameObject roadPrefab = roadPrefabs[randomIndex];

        // 实例化跑道
        Vector3 spawnPosition = new Vector3(0, 0, nextSpawnZ);
        GameObject newRoad = Instantiate(roadPrefab, spawnPosition, Quaternion.identity);

        // 添加到队列
        roadQueue.Enqueue(newRoad);

        // 更新下一个生成位置
        nextSpawnZ += roadLength;

        // 如果跑道数量过多,销毁最早的跑道
        if (roadQueue.Count > initialRoadCount + 2)
        {
            GameObject oldRoad = roadQueue.Dequeue();
            Destroy(oldRoad);
        }
    }
}
using UnityEngine;
public class Road : MonoBehaviour
{
    private GameObject player;
    private float destroyOffset = 6f;  // 销毁偏移量

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
    }

    void Update()
    {
        // 当玩家跑过跑道一定距离后销毁
        if (player != null && transform.position.z + destroyOffset < player.transform.position.z)
        {
            Destroy(gameObject);
        }
    }
}