Answers for "Projectile trajectory calculation for 3 dimensions"
http://answers.unity.com/questions/857780/projectile-trajectory-calculation-for-3-dimensions.html
The latest answers for the question "Projectile trajectory calculation for 3 dimensions"Answer by Firedan1176
http://answers.unity.com/answers/857996/view.html
What you could try is to get the velocity of the object you're shooting at and add that to the "target position" of your mechanism. So even if it's traveling away from the camera, it will still shoot at the correct location, but this implies that it takes exactly 1 second to reach the target no matter the distance.
I'm surprised it wasn't easy to find the answer, but a simple formula to calculate trajectory with the projectile's speed and the distance from the target:
float angle = (Mathf.Asin ((Physics.gravity.magnitude * distance) / (velocity * velocity)) / 2) * 100;
And you can incorporate this into a simple formula to find a target position.
public float bulletSpeed;
//This is purely for effect, just makes it rotate smooth
public float rotationSpeed;
//This is a custom script that calculates the velocity of an object.
//Because MY "test enemy" didn't have a rigidbody, I used code to calculate vel.
Vector3 enemyVelocity = enemy.GetComponent<Velocity>().velocity;
//Get the distance to the target as a float
float distance = Vector3.Distance (transform.position, enemy.transform.position);
//Get the number in seconds to reach the target.
float timeToReachTarget = (distance / bulletSpeed);
//This will be what we rotate towards (the "invisible target")
Vector3 targetPosition = (enemy.transform.position - transform.position);
//We will add the "tracking" stuff to make it aim up and ahead
targetPosition += (enemyVelocity * timeToReachTarget) + (Mathf.Asin((Physics.gravity.magnitude * dist) / (vel * vel)) / 2) * 100);
//This is purely for effect, just makes it rotate smooth
Quaternion tempRotation = Quaternion.LookRotation(pos1);
transform.rotation = Quaternion.Lerp(transform.rotation, tempRotation, rotationSpeed);
Problems:
If your enemy doesn't have a rigidbody, you need to use code to calculate.
If your enemy has a rigidbody, you need to get it and change the "Vector3 enemyVelocity" to get that rigidbody.velocity(???).
If your enemy moves randomly and not at a constant speed, it won't work well.
I hope this is what your wanting.
This is script I have made before and if it doesn't work for you, I will try to fix it. I can also include a link to the script I made if you want to look at that.Thu, 18 Dec 2014 21:48:58 GMTFiredan1176