[Wonderfully Illustrated + Code Supplied] Ambiguous question. Wrong beginning point for movement.

Hey! I would like to make a ball be thrown in a boomerang trajectory.

A script I found here (made by user named robertu) works but not perfectly. I changed a bit of code and still can’t get it right. The object seems to instantiate in the right place, but then starts its movement in the wrong place; makes the required trajoctory regardless of the rotation of the main (parent) object it was instantiated by, in my case - it was a mage.

Could you please help me? Thanks!

Here’s the picture:

Here’s the code:

using UnityEngine;
using System.Collections;

public class boomberang : MonoBehaviour {

	public GameObject throwobject;
	bool onetime;

	void Update () {
		if (Input.GetButtonDown ("Fire1")) {
			StartCoroutine(Throw(15.0f, 10.0f, transform.position, 2.0f));

	IEnumerator Throw(float dist, float width, Vector3 direction, float time) {

		Vector3 oldposition = Camera.main.transform.position;
		float height = Camera.main.transform.position.y;
		float timer = 0.0f;

		while (timer < time) {
			float t = Mathf.PI  * timer / time;
			float x = width * Mathf.Cos(t);
			float z = dist * Mathf.Sin (t);

			Vector3 newvector = new Vector3(x,height,z);

				if(onetime != true){
					throwobject = (GameObject)Instantiate (throwobject, oldposition, transform.rotation);
					onetime = true;

			throwobject.rigidbody.MovePosition(oldposition + newvector);
			timer += Time.deltaTime;
			yield return null;

	// '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.


the direction is locked to the axis and always facing forward, you will need to rotate new vector by the cameras (or whatever throwing the object)'s rotation.

 Vector3 newvector =  camera.transform.rotation * new Vector3(x,height,z);

this is how you rotate a vector, make sure you always put the Qauternion (the way unity represents a 3D rotation) before the Vector…

as for ignoring the rotation after the ball is thrown, it is even easier then the previous problem,

you simply store the rotation at the start of the co-routine so it doesn’t change when it is running

//we store it right at the start of the Enumerator function
Qaurternion oldRotation = camera.transform.rotation

//Apply it, remembering the Rotation Vector Rule
Vector3 newvector =  oldRotation  * new Vector3(x,height,z);

so like oldPosition and height, oldRotation will not change until the object is thrown again in contrast to transform.rotation. this way the ball only follows the path based on the initial rotation. I.e ignores your player completely

Hope it helps