- Home /
[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 function requires a direction vector and you are supplying it with a position. You might want to use transform.forward ins$$anonymous$$d. Also the direction vector is not actually used in the Throw function, so the start position will always only be based on the camera position. Is this the original script?
Upvote for writing a wonderfully illustrated question with code supplied.
yeah, the direction is locked to the axis and always facing formward, 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...
Guys, thank you very much for prompt, relevant answers! And I thank you for the upvotes. The initial code was written by robertbu in this topic: http://answers.unity3d.com/questions/606473/boomerang-system.html#answer-882331.
I tried what Fornoreason1000 suggested and it worked. Fornoreason1000, thank you very much!!!
I have another difficulty, though: is it possible to make a ball follow the curve, but not to react to the movement and / or rotation of a gameobject, which instantiated the ball, while IN the process of following the curve? Think about it in following way: you threw a ball and ran away, intuitevely you expect the ball to move separately from your movement. Could you please try to help me again?
Answer by Fornoreason1000 · Jan 24, 2015 at 06:34 AM
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