나는 Unity platformer 게임을 위해 적 AI 이동 시스템을 작업 중입니다. 일정한 기준으로 유휴, 오른쪽으로 이동 또는 왼쪽. 나는 결정을 결정한 사람이 다음 결정과 똑같을지라도 (즉, "오른쪽으로 이동"을 두 번 연속 선택하거나 그 이상의 횟수만큼 선택할 수 있더라도) 적의 결정을 선택할 수 있기를 바랍니다 싶어). 아래 스크립트는 오류가 없지만 게임을 테스트하면 적을 더듬 거리게 만듭니다. 때로는 두 번째로 오른쪽으로 이동 한 다음 왼쪽으로 이동합니다. 내 코드의 고유 한 논리가 다소 정확하다고 느껴지지만 구현 방식에 약간의 작업이 필요합니다. 네가 나에게 줄 수있는 도움에 감사한다.적의 인공 지능 이동 의사 결정
"Start"기능에 "MakeMovementDecision"기능을 넣으면 적을 .07을 왼쪽이나 오른쪽으로 이동하거나 정지 상태로두고 다른 이동 기능을 수행하지 않습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AIMovement : MonoBehaviour {
// References the enemy's Rigidbody2D component
private Rigidbody2D enemyRigidbody;
// Sets the enemy's movement speed
[SerializeField]
private int movementSpeed;
// Checks if the enemy is moving (to be used with animations)
private bool isMoving;
// The direction in which the enemy will move
private Vector2 directionToMove;
// The random decision (0, 1 or 2) that represents which movement function the enemy will perform
private int decisionValue;
// The time remaining before the enemy chooses which movement function to perform again
private float timeTilNextDecision;
// The random float that will be used to determine for how long the enemy remains idle
private float idleTime;
// The random float that will be used to determine for how long the enemy moves left or right
private float moveTime;
// Use this for initialization
void Start() {
// Accesses the enemy's Rigidbody2D component
enemyRigidbody = GetComponent<Rigidbody2D>();
}
void FixedUpdate() {
MakeMovementDecision();
}
/// <summary>
/// Generates the decision for which type of movement the enemy will perform
/// </summary>
private void MakeMovementDecision()
{
// Chooses a value upon which the movement decision will be based
decisionValue = Random.Range(0, 3);
switch (decisionValue)
{
// Keeps the enemy standing still
case 0:
Idle();
break;
// Moves the enemy to the right
case 1:
MoveRight();
break;
// Moves the enemy to the left
case 2:
MoveLeft();
break;
}
}
/// <summary>
/// Causes the enemy to stand still with idle animations
/// </summary>
private void Idle()
{
// Sets the idle stance duration
idleTime = Random.Range(5.0f, 10.0f);
// Calculates the time until the enemy may decide to change its movement
timeTilNextDecision = idleTime - Time.deltaTime;
// Sets the movement bool to false to play the idle animations
isMoving = false;
// Stops the enemy's movement
enemyRigidbody.velocity = Vector2.zero;
// Checks if the enemy should make a decision on its next movement
if (timeTilNextDecision < 0)
{
MakeMovementDecision();
}
}
private void MoveRight()
{
moveTime = Random.Range(2.0f, 5.01f);
timeTilNextDecision = moveTime - Time.deltaTime;
isMoving = true;
directionToMove = Vector2.right;
transform.Translate(directionToMove * (movementSpeed * Time.deltaTime));
if (timeTilNextDecision < 0)
{
MakeMovementDecision();
}
}
private void MoveLeft()
{
moveTime = Random.Range(2.0f, 5.01f);
timeTilNextDecision = moveTime - Time.deltaTime;
isMoving = true;
directionToMove = Vector2.left;
transform.Translate(directionToMove * (movementSpeed * Time.deltaTime));
if (timeTilNextDecision < 0)
{
MakeMovementDecision();
}
}
}
? 'FixedUpdate()'를 제거하고'Start()'안에'MakeMovementDecision()'함수를 넣으십시오 ... – 0014
예, 이것을 시도했습니다 (원래의 게시물을 편집하여 이것에 대한 참고 사항을 포함 시켰습니다). 0.07 단위로 왼쪽 또는 오른쪽으로 이동하거나 "유휴"변형을 선택하기 만하면 적이 결코 다시 이동하지 않습니다. 검사관의 x 값을 보지 않으면 적들이 전혀 움직이지 않는다고 인식 할 수 없습니다. – Dalsia