【问题标题】:Bullet not rotating in direction子弹不向方向旋转
【发布时间】:2018-12-04 07:51:36
【问题描述】:

我在 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);
    }
}

【问题讨论】:

    标签: c# unity3d game-physics


    【解决方案1】:

    尝试像这样更改您的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);
    }
    

    【讨论】:

    • 好的。子弹现在指向错误的方向。它们应该向左旋转 90 度。
    • transform.rotation = Quaternion.AngleAxis(90 + angle, Vector3.forward);
    • 我将 90 更改为 270。现在可以使用了。:) 谢谢老兄,你拯救了我的一天!
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多