我正在尝试构建一个游戏,其中AI需要远离玩家并继续收集地图上随机产生的收藏品。如何让它旋转到新生成的可收集对象?
玩家和对手都需要特别移动,如下面的代码所示。
if (touch.position.x < Screen.width / 2 && touch.position.y < Screen.height / 2) {
Player.Rotate (0, 0, 5);
}
if (touch.position.x > Screen.width / 2 && touch.position.y < Screen.height / 2) {
Player.Rotate (0, 0, 5);
}
对手也会根据旋转角度不断向前移动,因此只需要在正确的方向上旋转对手
void Update(){
transform.position += transform.up * Time.deltaTime * EnemySpeed;
}
这是我的对手AI的代码,可以找到收藏品的位置并向它旋转
void AIControls ()
{
if (GameManager.Instance.CollectibleReady) {
//rotate towards collectible Item
Vector3 opponentPos = transform.position;
foodPos.x = foodPos.x - opponentPos.x;
foodPos.y = foodPos.y - opponentPos.y;
angle = Mathf.Atan2 (foodPos.y, foodPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler (new Vector3 (0, 0, angle));
} else {
//Wander aimlessly avoid the player
}
}
这不能正常工作,对手不会转向新生成的收藏品。请帮助!
答案 0 :(得分:0)
我认为var directionToFood = foodPos - transform.position;
transform.rotation = Quaternion.LookRotation(directionToFood, Vector3.up);
可以帮助您 - http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
所以你可以这样写:
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
答案 1 :(得分:0)
好的,所以在玩了一会儿后我得到了我想要的东西,在这里添加代码给那些可能会觉得有用的人。
void AIControls ()
{
if (GameManager.Instance.CollectibleReady) {
//rotate towards collectible Item
Vector3 opponentPos = Camera.main.WorldToScreenPoint (this.transform.position);
Vector3 foodtPos = Camera.main.WorldToScreenPoint (food.transform.position);
Vector3 relativePos = foodPos - opponentPos;
float angle = Mathf.Atan2 (relativePos.y, relativePos.x) * Mathf.Rad2Deg;
this.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, angle - 90));
}
}
感谢大家调查此事!