- Home /
Instantiated Bullet Lags Behind Animated Timeline Object
Bullets are created on fire input with a rigidbody and an "AddForce". The ship is animated along a course via the new Timeline editor. But when the player shoots, the bullets aren't moving "with" the ship. I can speed them up to sort of "catch up" and take the ship over but It looks wrong and the bullets end up moving crazy fast.
Standing still the bullets work great, shooting out and away from the front of the ship. But moving, the animation is just too fast for the instantiated bullets and they don't count for how fast the ship is currently moving.
I also have other ship weapons that have fun stuff like shell casings that just vanish out of view because they can't keep up and I don't want them to shoot away like bullets by adding force to them. They need to be more lazy and slowly arc towards and away from the camera attached to the player.
In the image, the bullets are behind the ship when they should be coming out of the front and moving away from the ship, even while the ship moves along it's animated rail via the Timeline.
From what I understand, rigid bodies on the ship/children have a velocity of zero because the animation overwrites physics. so isntant.rigidbody.vellocity = ship.rigidbody.velocity has no effect...
Is there a way to child the bullets through the hierarchy so that they move normally (in front of the ship) instead of lag behind? So, as an example, the ship would have a child empty object where the bullets would nestle under as soon as they come into existence. That way the parent's motion would count towards the force and power of the prefab child bullet/shell casing/whatever I want to throw under it...
anyone have any suggestions on how to get this to work?
my bullet scripts are in two places.
First is the player input controller for instantiation:
Instantiate(bulletPrefab, bulletSpawner.transform.position, Quaternion.identity);
and bullet prefab itself:
public float shootPower = 5000f;
public Rigidbody rb;
public Rigidbody spawnerRb;
public GameObject bulletSpawnPoint;
public GameObject particlePretty;
void Start () {
bulletSpawnPoint = GameObject.Find("Bullet Spawn 1");
spawnerRb = bulletSpawnPoint.GetComponent<Rigidbody>();
rb = this.GetComponent<Rigidbody>();
rb.velocity = spawnerRb.velocity;
rb.AddRelativeForce(bulletSpawnPoint.transform.forward * shootPower);
... //and then the rest is die after x seconds function as well as a particle spawn on colliison
Answer by FortisVenaliter · Aug 31, 2017 at 09:36 PM
The first thing that comes to mind is that you're calling "AddRelativeForce" with a non-relative vector. You should use AddForce or simply Vector3.forward. Depending on how your scene is set up, that may actually be slowing them down.
I tried animating an empty game object then setting another object's transform to that. velocity of the non-animated object was zero as well.
What I'm asking is "how do I negate the speed and direction of an object on a pre-dertimed path/rail so that objects aren't spawned too slow to start and the force won't change depending on object's direction." For example, an object moving backwards with a forward firing bullet will perceive the bullet to fire faster than an object moving forward with the forward firing bullet.
The gif above shows a forward moving object shooting forward bullets.
Right, and it looks like you are doing that. I would set the initial velocity to the source velocity before adding force, and that's exactly what you're already doing.
Watching the values in the debug, velocity is zero if you aren't using forces on a rigid body. I tried calculating velocity via transform ( [old-new].magnitude / time.deltatime) but because it's based on the coordinate plane, that calculation isn't normalized and increases/decreases based on how far away you get from (0,0,0).
I thought about animating the bullets, but it would have the same effect unless if I wanted the bullets to move with the character/object ins$$anonymous$$d of the nice fire and forget that I have with the physics sim.
I'm thinking the only way to get the desired effect is to just slow the ship down. If it has a slower speed it will be easier to match the bullet's forward movement with the ship and not lag behind so much. Only issue is trying to figure out how to explain why the high-tech space ship is moving so slowly to the player...