子弹不按方向旋转
Bullet not rotating in direction
我在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);
}
我在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);
}