Search Unity

How to thrown a weapon and return to player

Discussion in '2D' started by YaKoM, Feb 15, 2017.

  1. YaKoM

    YaKoM

    Joined:
    Feb 15, 2017
    Posts:
    1
    Hi all, how can i do to throw a weapon and return again to the player position? like a boomerang. its for a 2D game.
    Thanks all.
     
    StarkAcrobat238 likes this.
  2. shreyrin

    shreyrin

    Joined:
    Nov 2, 2016
    Posts:
    1
    I dont have the exact code on me, but I have made a boomerang for a topdown game before.

    You need to have an object with a rigidbody2d and some type of boomerang script on it. You then need to add a timer variable on that script and count down from how long you want it to take. when the time is reached you find the player and set the velocity equal to the displacement, when the boomerang is within a certain range of the player destroy it.


    float timer;
    public float forwardTime = 1.0f;

    public void Update(){

    timer += time.deltaTime;
    if(timer >= forwardTime)
    getComponent<RigidBody2D>().velocity = findObjectOfType<PlayerController>().transform.position - transform.position;
    }
    }



    This is only a bit of the code, you will need to normalize the displacement so its travel rate doesnt change and add a destroy function.
     
    StarkAcrobat238 likes this.