how to throw object

Hi my enemies throw axes to hero ım using this code below to do this but when my hero is far direction vector gets bigger and axes move faster ı dont want to do that.How can ı make axes’ speed constant

Rigidbody2D rb;
    GameObject hero;
    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        hero = GameObject.Find("Main_hero");
    }

    // Update is called once per frame
    void Update()
    {
      
    }

    public void addspeed(GameObject axe)
    {
        float dx = hero.transform.position.x - transform.position.x;
        float dy = hero.transform.position.y - transform.position.y;
        Vector2 direction = new Vector2(dx, dy);

        rb.velocity = direction * 2;

    }

You need to normalize the direction so it is just a direction of length 1.

Also, you don’t need to do all this work, accessing each part of a vector as this is really expensive to do.

var direction = hero.transform.position - transform.position;
rb.velocity = direction.normalized * 2f
1 Like

thanks it worked

1 Like