61 lines
1.5 KiB
C#
61 lines
1.5 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 ApplyPositionOffset()
|
|
{
|
|
// 应用位置偏移量
|
|
transform.position += positionOffset;
|
|
}
|
|
}
|