I’ve got a projectile named “D1hook” moved by a Rigidbody’s “AddForce” command, and it’s fired by an AI named “dem1”, and it moves very quickly. I want it to be aimed at the player, which moves very quickly but not quite as quickly, and is also moved by a Rigidbody’s AddForce. I know how to make it aim at where the player currently is, but not where it will be by the time the D1hook has the time to catch up to it, and how to calculate when that will be. I need the D1hook Instantiated at the right rotation to hit the player when it reaches him.
all objects move in all 3 axis, but the D1hook doesn’t use gravity. I don’t want it to have a homing-in function.
This is ballistics 101. Here a basic iterative strategy.
- Figure out the time your projectile will take to reach the target.
- Figure out where the target will be at that time
- Recalculate the time to reach the target
- Repeat until close
You can also solve it analytically by rearanging the kinematic equations
Thanks for the conscious knowledge of the procedure. Here’s the code I had, which had some of that (evidently not all):
forward = dem1.transform.forward;
//gonna get the position, velocity and how much the velocity will increase as the game continues.
prevVol = curVol;
curVol = prb.velocity.magnitude;
playerdiff = curVol - prevVol;
//trying to get the frame to connect. This section is definitely wrong. How should I fix it?
for (var i = 0; i <= 100000; i++)
{
if (Vector3.Distance(Wewillbe * i, D1Hookcollidespot * i) > 1000)
{
fckingDist = Vector3.Distance(Wewillbe * i, D1Hookcollidespot * i);
collideframe = i;
}
}
// getting where the hook will spawn, its velocity and how much the velocity will increase, which is by 200 per frame.
if (D1hook != null)
{
disthook = Vector3.Distance(player.transform.position, D1hook.transform.position);
hookRB = D1hook.GetComponent<Rigidbody>();
hookspeed = hookspeed + 200;
frames = dist / hookspeed;
Wewillbe = prb.velocity.normalized + player.transform.position * prb.velocity.magnitude * playerdiff;
}
if (D1hook != null)
{
hookRB = D1hook.GetComponent<Rigidbody>();
HprevVol = HcurVol;
HcurVol = hookRB.velocity.magnitude;
Hplayerdiff = HcurVol - HprevVol;
Debug.Log("Hook change in Velocity: " + Hplayerdiff);
D1Hookcollidespot = dem1.transform.forward * 200 + transform.position;
}
dist = Vector3.Distance(player.transform.position, transform.position);
Debug.Log("Collide spot: " + collideatspot);
Cross = Vector3.Cross(Wewillbe * collideframe, collideatspot * collideframe);
lookatcol = Quaternion.LookRotation(Cross - transform.position, Vector3.up);
// supposed to instantiate the object pointing where the player will be
D1hook = Instantiate(dem1hook, gameObject.transform.position, lookatcol) as GameObject;
How should I fix it?