如何固定敌人以团结射击球员的方向
我试图让一个法师敌人在射程时用咒语射击。目前,他在正确的时间和正确的距离上拍摄了咒语弹丸,但是由于某种原因,它朝着球员相对于法师的相反方向发射。这是当前的代码。我是团结的新手,因此将不胜感激。
// Variables being used
public float period = 3f;
public GameObject spell;
public GameObject mage;
public GameObject HitBox;
public Transform LaunchOffset;
private float shootingTime;
private Transform playerTransform;
if (Time.time > shootingTime)
{
shootingTime = Time.time + period;
Vector3 myPos = new Vector3(LaunchOffset.position.x, LaunchOffset.position.y);
GameObject projectile = Instantiate(spell, myPos, Quaternion.identity);
Physics2D.IgnoreCollision(projectile.GetComponent<BoxCollider2D>(), mage.GetComponent<BoxCollider2D>(), HitBox.GetComponent<BoxCollider2D>());
Vector3 direction = myPos - (Vector3)playerTransform.position;
projectile.GetComponent<Rigidbody2D>().velocity = direction * spellSpeed;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目标点的方向是目标点的位置减去另一个点的位置。因此,只需更改那条代码即可:
以下是更多信息:
Direction of a point towards a target point, is target point's position minus other point's position. So just change that line of code:
Here is more info: https://docs.unity3d.com/2019.3/Documentation/Manual/DirectionDistanceFromOneObjectToAnother.html