Boomerang System

Hey everyone,

I have a mesh that I want to use as a boomerang. However, I’m trying to find a way to do this without animations, but with a collider (Box Collider to be exact) and the mesh will come back to you. Is there a way this is possible? I use C#, but I will also accept JavaScript. Thanks.

Here is a bit of code to start with. It uses the parametric equation for a ellipse for the throw.

using UnityEngine;
using System.Collections;

public class Boomerang : MonoBehaviour {

	void Update () {
		if (Input.GetKeyDown (KeyCode.Space)) {
			StartCoroutine(Throw(18.0f, 1.0f, Camera.main.transform.forward, 2.0f));
		}
	}

	IEnumerator Throw(float dist, float width, Vector3 direction, float time) {
		Vector3 pos = transform.position;
		float height = transform.position.y;
		Quaternion q = Quaternion.FromToRotation (Vector3.forward, direction);
		float timer = 0.0f;
		rigidbody.AddTorque (0.0f, 400.0f, 0.0f);
		while (timer < time) {
			float t = Mathf.PI * 2.0f * timer / time - Mathf.PI/2.0f;
			float x = width * Mathf.Cos(t);
			float z = dist * Mathf.Sin (t);
			Vector3 v = new Vector3(x,height,z+dist);
			rigidbody.MovePosition(pos + (q * v));
			timer += Time.deltaTime;
			yield return null;
		}

		rigidbody.angularVelocity = Vector3.zero;
		rigidbody.velocity = Vector3.zero;
		rigidbody.rotation = Quaternion.identity;
		rigidbody.MovePosition (pos);
	}
}

'dist' - distance of the throw.
'width' - width of the ellipse for the throw.
'direction' - vector indicating the direction of the throw
'time' - time the throw should take.

It all depends on whether you want the boomerang to move in 3d or 2d and how realistic you want it to be. If it’s a Castlevania type game, you just want it to go in the direction it is thrown, and add the opposite direction back to it every frame multiplied by some amount, which will make it slow down, reverse, and speed up as it comes back.

If it’s more realistic 3d, a boomerang will follow a rather complex path where it goes straight, veers upward, makes a vertical u-turn, levels out and comes back in a curve.
In this scenario, you may want to give it a rigid body with an initial velocity create several Vector3s in world space using playersTransform.TransformDirection at the time it is thrown, and then use the elapsed time percentage to Lerp between those directions and add the result to its velocity in FixedUpdate.