【发布时间】:2019-09-07 14:21:54
【问题描述】:
我正在用怪物(RPG)制作游戏,我想做的是怪物左右移动(巡逻),等待一段时间后它会静止不动(空闲)。 所以一开始就是怪物:
- 移动 5 秒,
- 然后变为空闲,在空闲 3 秒后,
- 它又动了
这个过程一次又一次地无限重复,我写的代码只是每1秒不停地改变Idle和Patrol,我不知道,希望你能帮我弄清楚,谢谢!
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterMove : MonoBehaviour { [SerializeField] private float
MinPatrolTime, MaxPatrolTime, MinIdleTime, MaxIdleTime; [SerializeField]
private bool ShouldBeIdle=false; public float MonsterMoveSpeed; public
float Distance=2f; [SerializeField] private bool ShouldBePatroling =
true; private bool MovingRight = true;
public Transform groundDetection;
private void Start()
{
}
private void Update()
{
}
void FixedUpdate()
{
if (ShouldBePatroling&&!ShouldBeIdle)
{
Move();
StartCoroutine(PatrolTime());
}
else if (ShouldBeIdle==true&&!ShouldBePatroling) { //Idle Anim
Debug.Log("Doing Idle Anim");
StartCoroutine(IdleTime());
}
}
void Move()
{
transform.Translate(Vector2.right * MonsterMoveSpeed * Time.deltaTime);
int layer_mask1 = LayerMask.GetMask("Ground");
RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position,
Vector2.down, Distance, layer_mask1);
if (groundInfo.collider == false)
{
if (MovingRight == true)
{
transform.eulerAngles = new Vector3(0, -180, 0);
MovingRight = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
MovingRight = true;
}
}
}
IEnumerator PatrolTime()
{
yield return new WaitForSeconds(Random.Range(MinPatrolTime,
MaxPatrolTime));
ShouldBePatroling = false;
ShouldBeIdle = true;
}
IEnumerator IdleTime()
{
yield return new WaitForSeconds(Random.Range(MinIdleTime, MaxIdleTime));
ShouldBePatroling = true;
ShouldBeIdle = false;`
}
【问题讨论】:
标签: c# unity3d animation artificial-intelligence