So I am trying to make a projectile that when hitting a object it goes back to the player. Can someone tell me how to run the StartCoroutine function when a event happens until another event happens?
Any reason you’re not just using a tweener to bring back your boomerang??
I use iTween because it’s super-simple, one file.
Otherwise, make the coroutine run until it detects the target is back at its destination, which is precisely what tweeners already do.
You can have a bool in your script that is true when the coroutine should be active and false when not. Let’s call this bool coroutineActive. In your coroutine method you can then use:
while(coroutineActive) { }
or
if (!coroutineActive) yield break;
You can also just call StopCoroutine(), probably simpler this way actually.
I did not know iTween existed
How would I use it in this code to make stuff bounce back to the player?
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.layer == 6) return;
if (targetHit) return;
else targetHit = true;
Debug.Log("hit");
rb.linearVelocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
transform.LookAt(player.transform);
StartCoroutine(RightBackAtYa(Player));
}
public IEnumerator RightBackAtYa(GameObject player)
{
transform.position += (player.transform.position - transform.position).normalized * speed * Time.deltaTime;
transform.LookAt(player.transform);
yield return null;
}
}
You should play with it… tweening is FUN!!
As soon as you start tweening your brain will start thinking of new game ideas.
It would be something like:
iTween.MoveTo( boomerangGameObject, destinationVector3, durationFloat);
That’s the simple and convenient “go here in this amount of time” variant.
The full variant accepts a hash table that can be populated with a ridiculous number of various arguments, such as this simple example I use for moving a card to a discard pile and calling a method:
iTween.MoveTo(card.gameObject, iTween.Hash(
"position", DiscardPile.position,
"easeType", "easeOutQuart",
"time", travelTime,
"delay,", delay,
"oncomplete", "Completed"
));
You might want to also set your Rigidbody to Kinematic during the duration, but sure what all you’re attempting to do there.
Thank you iTween is very useful
BTW - if you just want to send the object back in the direction it came from then you can do this:
void OnCollisionEnter(Collision collision)
{
rb.linearVelocity = collision.relativeVelocity;
}