Files
aibis-dream/Assets/Scripts/AnimationManage/MoveableVehicle.cs
T
2025-03-12 00:03:39 +08:00

87 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveableVehicle : MovableObject
{
public bool shouldStop;
public float stopTime;
private bool isStopped = false;
public bool defaultFacingRight = true; // 车辆默认朝向是否向右
public Vector3 positionOffset = Vector3.zero; // 位置偏移量
protected override void Initialize()
{
shouldStop = Random.value > 0.5f; // 50% 概率停留
stopTime = Random.Range(1f, 3f);
AdjustFacingDirection();
//SetWheelRotation();
ApplyPositionOffset();
}
protected override void Update()
{
if (!isStopped)
{
Move();
}
}
private IEnumerator StopCoroutine()
{
isStopped = true;
yield return new WaitForSeconds(stopTime);
isStopped = false;
}
public void StopAndWait()
{
if (shouldStop)
{
StartCoroutine(StopCoroutine());
}
}
private void AdjustFacingDirection()
{
// 如果生成在左边,朝向右;如果生成在右边,朝向左
bool movingRight = transform.position.x < 0;
bool shouldFlip = (defaultFacingRight && !movingRight) || (!defaultFacingRight && movingRight);
transform.localScale = new Vector3(shouldFlip ? -1 : 1, 1, 1);
direction = movingRight ? Vector2.right : Vector2.left;
}
private void SetWheelRotation()
{
// 从 Resources 加载材质
Material wheelMaterial = Resources.Load<Material>("Materials/BigCarRoation");
if (wheelMaterial == null)
{
Debug.LogError("Material 'BigCarRoation' not found in Resources/Material.");
return;
}
float rotationSpeed = direction.x > 0 ? 1f : -1f; // 右为正,左为负
foreach (Transform child in transform)
{
Renderer renderer = child.GetComponent<Renderer>();
if (renderer != null)
{
foreach (var material in renderer.materials)
{
// 应用加载的材质
material.CopyPropertiesFromMaterial(wheelMaterial);
material.SetFloat("_UVRotateSpeed", rotationSpeed);
}
}
}
}
private void ApplyPositionOffset()
{
// 应用位置偏移量
transform.position += positionOffset;
}
}