gpt4 book ai didi

c# - 子弹不按方向旋转

转载 作者:行者123 更新时间:2023-11-30 21:33:45 35 4
gpt4 key购买 nike

我在 Unity 中的子弹方向是这样的:

direction = bullet01.transform.position - this.transform.position;

方向是一个 Vector2。

问题是我的子弹没有朝它飞行的方向看。

这是我的子弹:

public void SetDirection (Vector2 direction)
{
//set the direction normalized, to get an unit vector
_direction = direction.normalized;
}

// Update is called once per frame
void Update () {
//get the bullet's current position
Vector2 position = transform.position;

position += _direction * speed * Time.deltaTime;

//update the bullet's position
transform.position = position;

//this is the top right point of the screen
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));

//if the bullet went outside the screen on the top, then destroy the bullet
if (transform.position.y > max.y) {
PlayerControl.bulletPool.ReturnInstance(gameObject);
}
}

最佳答案

尝试像这样改变你的 SetDirection

public void SetDirection (Vector2 direction)
{
//set the direction normalized, to get an unit vector
_direction = direction.normalized;

float angle = Mathf.Atan2(_direction.y, _direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}

关于c# - 子弹不按方向旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51028527/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com