【发布时间】:2018-11-26 20:25:19
【问题描述】:
我正在制作一个左右两侧的 2D 游戏,因此玩家只能向左或向右移动或跳跃。我做了一个AI作为敌人。人工智能工作正常。他可以去找玩家杀死他。
问题是敌人在杀死玩家时无法旋转或面对玩家。我想让 AI 看着玩家。当玩家在敌人的左边时,敌人应该旋转到左边看玩家。我搜索了许多网站,但没有得到任何正确的解决方案。这是我的敌人脚本:
public class enemy : MonoBehaviour
{
public float speed;
private Animator animvar;
private Transform target;
public GameObject effect;
public float distance;
private bool movingRight = true;
public Transform groundedDetection;
public float moveInput;
public bool facingRight = true;
void Start()
{
animvar = GetComponent<Animator>();
target = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(target.position, transform.position) < 20)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
else
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
RaycastHit2D groundInfo = Physics2D.Raycast(groundedDetection.position, Vector2.down, distance);
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals("danger"))
{
Instantiate(effect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
if (col.gameObject.tag.Equals("Player"))
{
Instantiate(effect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
游戏是 2D,左,右,使用 Unity3D 跳跃。
【问题讨论】:
-
一个清晰明确的问题会有所帮助!