I am trying to make the main character throw a projectile and it comes right back the way it came but i really dont know how to accomplish this, I thought maybe I can say if it reaches a distance from point 0,0 it can come back but that didnt work. i just want it to go in straight line forward and a straight line back.
Im doing it in C# but any help will do.
Yes, the distance idea is right. You can do something like :
public float speed = 1f; // default speed 1 unit / second
public float distance = 5f; // default distance 5 units
public Transform boomerang; // the object you want to throw (assign from the scene)
private float _distance; // the distance it moves
private bool _back; // is it coming back
public void Shoot ()
{
_distance = 0; // resetting the distance
back = false; // resetting direction
enabled = true; // enabling the component, so turning the Update call on
}
private void Update ()
{
float travel = Time.deltaTime * speed;
if (!back)
{
boomerang.Translate(Vector3.forward * travel); // moves object
_distance += travel; // update distance
back = _distance >= distance; // goes back if distance reached
}
else
{
boomerang.Translate(Vector3.forward * -travel); // moves object
_distance -= travel; // update distance;
enabled = _distance > 0; // turning off when done
}
}
private void OnEnable ()
{
boomerang.gameObject.SetActive (true); // activating the object
}
private void OnDisable ()
{
boomerang.gameObject.SetActive (false);
}