2D Boomerang Style Weapon
So i've been trying to make a weapon that essentially behaves like a boomerang, being launched from the player and then returning to them.
So far i've been using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowerBoomerang : MonoBehaviour {
public float speed = 10;
bool returning = false;
private Transform playertrans;
float boomerangTimer;
void OnEnable(){
playertrans = GameObject.Find ("Player").transform;
boomerangTimer = 0.0f;
}
// Update is called once per frame
void Update () {
boomerangTimer += Time.deltaTime;
if (boomerangTimer >= 1f) {
returning = true;
}
if (!returning) {
transform.Translate (Vector2.right * speed * Time.deltaTime);
} else {
transform.right = playertrans.position - transform.position;
transform.Translate (Vector2.right * speed * Time.deltaTime);
}
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player") {
speed = 0;
}
if (other.tag == "Enemy") {
returning = true;
}
}
}
Which works great in a singular format, aka destroying the boomerang and spawning another.
What I want is for the boomerang to never need to be destroyed, and simply for it to return to an idle state that can then be fired again.
The issue I have is that upon returning to the player, the boomerang glitches out, moving uncontrollably and refusing to fire again, even if the code is enabled/disabled.
If anyone could advise me on how to fix this / improve the concept that would be great!
Thank you in advance
Comment