drpelz
drpelz

Reputation: 811

Bullet not rotating in direction

I have this direction of my bullet in Unity:

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

The direction is a Vector2.

The problem is that my bullet doesn't look in the direction it's flying.

This is my bullet:

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

Upvotes: 2

Views: 239

Answers (1)

Fredrik Widerberg
Fredrik Widerberg

Reputation: 3108

Try changing your SetDirection like this

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

Upvotes: 3

Related Questions