(サンプルコード)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BotTurnSlow : MonoBehaviour
{
private float moveSpeed = 3f;
private bool isRotating = false; // 旋回中かどうか
void Update()
{
if (!isRotating)
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
}
// 境界線に触れたときの処理
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Wall") && !isRotating)
{
StartCoroutine(RotateRandomly());
}
}
IEnumerator RotateRandomly()
{
isRotating = true; // 旋回開始フラグを立てる
// ★改良
for (int i = 0; i < 10; i++)
{
transform.rotation = transform.rotation * Quaternion.Euler(0, i * 5, 0);
yield return new WaitForSeconds(0.1f);
}
isRotating = false; // 旋回終了フラグを下ろす
}
}