I’m pretty new at Unity, and I’m building a game that can best be summed up to play like Starfox for N64. I have a main character that moves around the screen that is constantly progressing through a level, and the user can shoot enemies out of the sky when they click a point in 3d space. I am having some pretty bad issues in getting the launcher to work properly. I want to contain everything in a game object (enemies, player, etc), and then propel this container through the level following curves I set up for it.
Problem is, while this container is moving (works perfect standing still), when I click to shoot enemies (from a launcher attached to my main object), the rigidbody projectile that is supposed to aim at the mouse target does not inherit the launcher’s parent forward velocity, and the instance is created without a parent. Is it possible to instantiate these to move completely relative to the launcher itself? Or do rigidbodies only perform physics based on the whole world??
I’m not sure what you mean by ‘the instance is created without a parent’, but as for your general question, there’s a variety of ways you can get a rigid body-based object moving in the right direction once it’s instantiated.
One method would be to instantiate the projectile with the same orientation as the ‘launcher’ game object. Assuming the launcher is pointing in the right direction, this means that after instantiation, the projectile’s forward direction vector will also be pointing in the right direction. Then, on the first FixedUpdate() call for the projectile, you can apply an impulse in the forward direction (and/or apply a constant force every fixed update, if you want the projectile to accelerate over time).
Hmm, I think my question might have an even more basic solution than that. I have it set up right with the launcher pointing toward where it needs to fire, but I think my issue might lie with the instantiated object not being parented to the launcher (therefore, not moving relative to the moving object, but instead to the world).
Any thoughts? I definitely want the object to move relative to the launcher, no to world space. How should I go about parenting The instantiated object? When I just parent it through code, it still behaves relative to world space and doe not move with the launcher when I move the launcher. This probably has a really easy solution so I apologize, but I’m just new with Unity
I don’t think I’m understanding what you’re wanting exactly, 'cause typically you would not make a projectile a child object of the object that launched it.
Imagine a launcher that launches a missile; upon launch, the missile is made a child object of the launcher. The launcher does not move (initially) after launching the missile, so the missile moves forward steadily as you would expect.
A short time later the missile is a mile away. At this point, the launcher rotates 90 degrees in preparation to launch another missile. If the first missile is a child object of the launcher, and is moving relative to the launcher, when the launcher rotates 90 degrees, the missile will all of a sudden ‘whip’ sideways along a mile-plus-long 90-degree arc. After this, the missile will still be moving forward relative to the launcher (for example, if previously it had been a mile north of the launcher and moving north, now it’s a mile east of the launcher and moving east).
Obviously this isn’t how things work in real life, which is why it’s seldom modeled this way in computer simulations. So, I have some doubt as to whether parenting is really what you’re looking for here, but maybe it is - again, I don’t really understand the problem you’re trying to solve.
Sure, just assign the velocity of the rigid body of the object that’s spawning the projectile to the rigid body of the spawned projectile. Can’t guarantee I’ll get the syntax right off the top of my head, but in C# at least it might look something like this: