- Home /
Issues when instantiating moving object from other moving object
I'm making a prototype of a top-down space RPG in which the player can fire weapons from a controllable spaceship - I've run into some issues with the projectiles fired though. Namely, if I fire (instantiate) a projectile from the spaceship when it stands still everything works, but if I fire it while the spaceship moves it goes significantly slower than it should and stutters quite a bit.
I move the spaceship by adding a force to a rigidbody of the ship model, and I make the projectile (in this case a laser beam) move by simply changing its forward position every second.
Here's the code I use, for the spaceship:
//Calculate target position
targetPosition = controlCamera.ScreenToWorldPoint (Input.mousePosition);
//Add force in direction
rigidbody.AddForce (targetPosition*shipSpeed*Time.deltaTime;);
And for the projectile:
transform.position = transform.position + transform.forward * speed * Time.deltaTime;
Both in the Update function. I tried using the FixedUpdate function instead for either or both, no difference there.
Any idea why the projectile might be traveling too slow and stutter when fired as the ship moves? It fires straight out from the front of the ship, but significantly faster than the ship's max speed - yet when firing forward on the move it actually moves way slower than the ship.
You are mixing rigidbody movement (ship) and the movement by direct transform (projectile). This problem would be much easier to solve if the projectile was a rigidbody. You can try having the ship add its speed to the 'speed' variable for the projectile at launch. Assu$$anonymous$$g the ship is always traveling on its forward and the projectile is launched in the the forward direction, you can add rigidbody.velocity.magnitude to the 'speed' variable of the projectile.
Answer by sparkzbarca · Dec 24, 2013 at 02:02 AM
well to start with, when dealing with lasers you should be using raycasting and drawline to just draw a quick planar object between you and the targets, lasers act instantly so there easy to emulate.
If you want to deal with projectile well yes you'll use a rigid body, you'll make its speed on spawn equal to the current speed of the ship + the base projectile speed.
Answer by ivarhill · Dec 24, 2013 at 10:23 AM
Thanks for the answers!
This particular laser weapon is not meant to be very realistic - rather a fast traveling projectile as seen in many sci-fi interpretations, so a raycast would not be suitable in this specific scenario.
I'm currently having some issues despite the changes I've made though... the projectiles seem to go slower than they should, definitely far slower than the ship no matter if it moves or not. Of course the mass of the projectile works into it too, but I'll have many different projectiles with different weights, so it might be a bit complicated...
This is my current projectile code (omitting irrelevant parts):
void Start () {
speed = speed + GameObject.FindGameObjectWithTag("ship").rigidbody.velocity.magnitude;
rigidbody.AddForce(transform.forward * speed* Time.deltaTime);
}
Your answer

Follow this Question
Related Questions
Tank projectile speed 2 Answers
3D Space Shooter Firing Projectiles 0 Answers
How can you instantiate a prefab with an initial burst of speed using ForceMode2D.Impulse? 2 Answers
Click button to fire weapon. 3 Answers
Having problems with Instantiate. 2 Answers