1
최근 유니티에서 게임을 만드는 데 관심이 생겼지 만 언어가 어떻게 작동하는지 전혀 모른다 (C#).적 AI가 움직이지 않는다
내가 만든이 현재의 테스트 "게임"은 적을 혼자서 돌아 다니게 만드는 것입니다. 내가 원하는 코드를 만들었지 만 불행히도 적들은 움직이지 않을 것입니다. 나는 어떤 오류도 발생하지 않고 누군가가 도울 수 있기를 바라고 있기 때문에 무엇이 잘못되었는지 확신하지 못합니다. Debug.Log 값은 모두 반환해야하지만 적군은 이동하지 않습니다.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
private void Start(){
findPosition();
}
//Variables
public Transform enemy;
private Vector2 position;
/* not necessary
private float maxPosX;
private float maxPosY;
private float maxNegX;
private float maxNegY;
*/
public float wanderSpeed = 3;
private float totalWalkDistanceUp;
private float totalWalkDistanceDown;
private float totalWalkDistanceLeft;
private float totalWalkDistanceRight;
private void findPosition(){
position.x = enemy.position.x;
position.y = enemy.position.y;
/* not neccessary
maxPosX = position.x + 5;
maxNegX = position.x - 5;
maxPosY = position.y + 5;
maxNegY = position.y -5;
*/
Debug.Log ("Enemy position: " + position);
//Debug.Log ("Max positive 'x' position: " + maxPosX + ". Max positive 'y' position: " + maxPosY + ". Max negative 'x' position: " + maxNegX + ". Max negative 'y' position: " + maxNegY);
Debug.Log ("Finished!");
enemyWalk();
}
private void enemyWalk(){
//Distance Generator
float walkDistance = Random.Range (1f, 5f);
totalWalkDistanceUp = walkDistance + position.x;
totalWalkDistanceDown = position.x - walkDistance;
totalWalkDistanceRight = walkDistance + position.y;
totalWalkDistanceLeft = position.y - walkDistance;
Debug.Log ("Distance generated up: " + totalWalkDistanceUp + ". Distance generated down: " + totalWalkDistanceDown + ". Distance generated left: " + totalWalkDistanceLeft + ". Distance generated right: " + totalWalkDistanceRight);
//Direction generator
int directionGenerator = Random.Range (1, 4);
if(directionGenerator == 1){ //up
enemy.transform.Translate(new Vector2(totalWalkDistanceUp, 0) * wanderSpeed * Time.deltaTime);
Debug.Log ("Direction generated: up");
}
else if(directionGenerator == 2){ //down
enemy.transform.Translate(new Vector2(totalWalkDistanceDown, 0) * wanderSpeed * Time.deltaTime);
Debug.Log ("Direction generated: down");
}
else if(directionGenerator == 3){ //left
enemy.transform.Translate (new Vector2(0, totalWalkDistanceLeft) * wanderSpeed * Time.deltaTime);
Debug.Log ("Direction generated: left");
}
else if(directionGenerator == 4){ //right
enemy.transform.Translate(new Vector2(0, totalWalkDistanceRight) * wanderSpeed * Time.deltaTime);
Debug.Log ("Direction generated: right");
}
}
}
전체 코드 : http://www.pastebucket.com/75289
이 올바르게 작동 :
Update
은 너무Update
내부enemyWalk()
는 캐릭터의 움직임을 볼 수 호출 한 번 프레임마다 호출됩니다? 캐릭터가 조금 빨리 움직이면, 약 6 초 동안 멈춰서 다시 움직여야 의미가 있습니다. – Jeff[InvokeReapating] (http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html) ("enemyWalk", 0,1) 사용할 수 있습니다. –
움직임을 time.deltaTime에 곱하십시오 (그래서 프레임 속도와 독립적 일 수 있습니다). 너무 빠르면 매번 결과를 나눕니다. 다른 해결책은 Update() 대신 FixedUpdate()를 사용하고 프로젝트 설정에서 FixedUpdate()에 대한 시간 단계를 수정하는 것입니다. – Gounemond