- Home /
Find the right angle for certain distance with arc effect?
Hi,
i have read that to find a trajectory, we can use this equation with pointA and pointB (AB = distance) :
AB = v0^2 sin2a / gravity
i am looking only for the angle, as i will have a constant speed to throw the ball, so depending on the distance, how could i find the right angle?
I found this thread : http://answers.unity3d.com/questions/145972/how-to-make-enemy-canon-ball-fall-on-mooving-targe.html
but the function uses a different velocity compared to the angle, i would need to use the same velocity. So i would like to find the angle automatically when we have the distance, so that the ball can start coming down at the middle of pointA and pointB.
Is the equation above correct to find this in a 3d environment?
If so, i thought i could calculate it like this : sin2a = AB g / v0^2
but then i am not quite sure how to go on to only find "a", should i calculate the result of AB g / v0^2 , then :
var distArc : float = Vector3.Distance(to.position, thisTransform.position);
var sin2a : float = (distArc * -1) / (speedArc*speedArc);
Debug.Log(Mathf.Asin(sin2a));
and divide the total by 2?
The result in the Debug.Log is something like 0.2, 0.4, until it reaches the target (i only tested with a simple addForce in the forward direction), i am not sure if i am following a wrong way or not...
Thanks
If you have the vector already,I would think you only need the angle with some world vectors like so:
Vector3.Angle(theVec, Vector3.forward);
Is that so ?
@fafase but i would need more to calculate the right angle for the right distance, i thought i could just stop the addForce when the middlepoint between A and B has been passed, but i miss the logic to find the right angle. Vector3.Angle would just give me the angle between 2 characters (x and z because both meshes have the same y) but not how much angle i would need to throw the ball in the air and make it come down at pointB
Ok you may want to have a look at this page http://unitygems.com/mistakes1/
Check out the rigidbody section. There is a AngryBird like demo. You will see that the ball goes depending on the angle made by the mouse. $$anonymous$$aybe that will give you an idea.
@fafase thanks fafase, the link with angrybird is actually just a direction you take from the end point of the mouse, and then you add a force to this direction. I haven't found yet, but i am trying something, i set the same angle/direction for all kind of distance, and calculate when we're at the middle of the distance, if so i change the direction towards the target, i haven't tried yet, if it works ok i will confirm. ;) Thanks for the comments anyway, if anybody sees this thread and thinks of any other idea, please post, your idea/answer is welcome! ;)
Answer by Pauls · Dec 03, 2012 at 12:37 AM
So, this is not the best approach i think, but it kind of works, i used a characterController instead of a Rigidbody, and tell the ball to change its orientation comparing to the distance it has already made. If someone finds a good approach, he is welcome to share :) Hope this helps anyway :
function Start () {
thisTransform = GetComponent(Transform);
character = GetComponent(CharacterController);
distM = Vector3.Distance(cube.position, thisTransform.position);
startPoint = thisTransform.position;
//rotation to parallel
parallel = thisTransform.rotation;
parallel.eulerAngles = Vector3(0, 0, 0);
}
function Update () {
var dir : Vector3 = cube.position - thisTransform.position;
//if 1/3 of distM
if (Vector3.Distance(startPoint, thisTransform.position) > distM/3 ){
if (Vector3.Distance(startPoint, thisTransform.position) < distM/2){
//from 1/3 to middle
thisTransform.rotation = Quaternion.Slerp( thisTransform.rotation, parallel, Time.deltaTime *speedRot );
} else {
//from middle to end
thisTransform.rotation = Quaternion.Slerp( thisTransform.rotation, Quaternion.LookRotation(dir), Time.deltaTime *speedRot );
}
}
if (Input.GetAxis("Horizontal")>0) yes = true;
if (yes) {
character.Move(thisTransform.forward * Time.deltaTime *30);
}