Hello! i am trying to create a boomerang weapon for my game and yet i cant figure out how to make
it return to the player.
using UnityEngine;
public class Boomerangitem : MonoBehaviour
{
public float Damage = 100f;
private bool collided;
public bool theytouched;
public GameObject Holder;
public GameObject Boomerrang;
public float speed;
private void OnCollisionEnter(Collision co)
{
//if he hasnt collided and then collides with something delete
if (co.gameObject.tag != "bullet" && co.gameObject.tag != "Player" && !collided)
{
collided = true;
theytouched = true;
}
if (co.gameObject.tag == "Enemy")
{
Enemy enemy = co.gameObject.GetComponent<Enemy>();
enemy.TakeDamage(Damage);
theytouched = true;
}
}
private void Update()
{
if (theytouched)
{
Boomerrang.transform.position = Vector3.MoveTowards(Boomerrang.transform.position, Holder.transform.position, speed);
}
}
}
and yet it doesnt return to the player
here is another script for the shooting aspect of the boomerang script
using UnityEngine;
using System.Collections;
using TMPro;
public class boomerang : MonoBehaviour
{
//Pubfloats
public float damage = 10;
public float range = 100;
public float fireRate = 15f;
public float arcRange = 1f;
public float Projectilespeed = 30f;
public float spread;
public float reloadTime = 1f;
//Camera
public Camera cam;
//Pub objects
public Animator animator;
public TextMeshProUGUI DelayDisplay;
public GameObject projectile;
public Transform Firepoint;
//Pub Bools
public bool Isthrown = false;
//priv floats
private float nextTimeToFire = 0f;
//priv Vector
private Vector3 destination;
private void Start()
{
}
private void OnEnable()
{
Isthrown = false;
animator.SetBool("Reloading", false);
}
// Update is called once per frame
void Update()
{
//shoots the gun
if (Input.GetButton("Fire2") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f/fireRate;
Shoot();
}
}
void Shoot()
{
if (Isthrown == false)
{
//shoots if the gun is a projectile weapon
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
destination = hit.point;
else
destination = ray.GetPoint(1000);
Isthrown = true;
InstantiateProjectile(Firepoint);
//Creates teh projectile and makes it move
void InstantiateProjectile(Transform firePoint)
{
var projectileObj = Instantiate(projectile, firePoint.position, Quaternion.identity) as GameObject;
projectileObj.GetComponent<Rigidbody>().linearVelocity = (destination - firePoint.position).normalized * Projectilespeed;
iTween.PunchPosition(projectileObj, new Vector3(Random.Range(-arcRange, arcRange), Random.Range(-arcRange, arcRange), 0), Random.Range(0.5f, 2));
}
}
}
}
any help or feedback is gladly appericated