【发布时间】:2020-12-15 13:35:41
【问题描述】:
我创建了一个小地图,并在小地图(右上角)中显示了敌人的当前位置,因此我创建了导航图像并附加为敌人 GameObject 的子对象。
但是导航图像也会随着它的父游戏对象(敌人)一起旋转,在小地图中也是如此。我希望导航图像仅在 x 轴上旋转,以便在小地图中正确可见。
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class EnemyFollow : MonoBehaviour {
private RaycastHit hit;
NavMeshAgent mAgent;
public Transform player;
private TargetPlayer target;
public GameObject impactEffect;
public int damageToPlayer = 10;
private int range = 100;
void Start() {
mAgent = GetComponent<NavMeshAgent>();
}
void Update() {
transform.LookAt(player);
if (Physics.Raycast(transform.position, transform.forward, out hit, range)) {
if ("Player".Equals(hit.transform.tag)) {
//follow player
mAgent.destination = player.position;
//shoot bullet
GameObject impactDestroy = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactDestroy, 1f);
//damage to Player
target = hit.transform.GetComponent<TargetPlayer>();
if (target != null) {
target.TakeDamage(damageToPlayer);
}
}
}
}
}
【问题讨论】:
标签: c# unity3d rotation parent