- Home /
How to make an object launched and landed on a target with gravity pull (Half parabolic trajectory) ?
Hi everyone, I am following a tutorial here https://vilbeyli.github.io/Simple-Trajectory-Motion-Example-Unity3D/
I'd like to modify the Launch ()
function by starting the object from top (see image). So half of the parabolic trajectory. So I removed the Vy
thinking I would not need it. But the result is not accurate (didn't reach the target).
Can anyone help? Thank you so much
private void Launch()
{
Vector3 pos = transform.position;
Vector3 target = dot.position;
distance between target and source
float dist = Vector3.Distance(pos, target);
rotate the object to face the target
transform.LookAt(target);
// calculate initival velocity required to land the cube on target using the formula (9)
float Vi = Mathf.Sqrt(dist * -Physics.gravity.y / (Mathf.Sin(Mathf.Deg2Rad * _angle * 2)));
float Vy, Vz; // y,z components of the initial velocity
//Vy = Vi * Mathf.Sin(Mathf.Deg2Rad * _angle);
Vz = Vi * Mathf.Cos(Mathf.Deg2Rad * _angle);
// create the velocity vector in local space
Vector3 localVelocity = new Vector3(0f, 0f, Vz);
// transform it to global vector
Vector3 globalVelocity = transform.TransformVector(localVelocity);
// launch the cube by setting its initial velocity
GetComponent<Rigidbody>().velocity = globalVelocity;
// after launch revert the switch
_targetReady = false;
}`
Answer by akillingbeck · Sep 04, 2017 at 12:43 PM
All you had to do is move your object to that position and then call launch. It calculates from it's current position in the launch function. No need to change the function. (assuming the function actually works initially)
I did, but it didn't work. It went over the target. I assume it's because it has Vy
that pushed it up more than it needed ( Vy
was needed if launch from ground to make an arc).