- Home /
Instantiate Angle from a Child Object
I have been trying for a couple of hours now, in an attempt to instantiate an object at the location of a child object, whose parent has the script attached. However the instantiate angle doesn't seem to be outputting the rotations I would expect.
The parent object is an empty game object that follows a path and has the script for all player actions attached to it. This makes life millions of times easier in every facet thus far. The child object is the player character. It is what collides into obstacles and enemy projectiles, as well as firing projectiles itself.
This is where the problem lies, firing the projectiles at the appropriate angle.
As time goes on the parent object rotates and moves around the map (rotation around y only). The child object rotates around y freely, under the players control. The parent has the script that instantiates the projectiles at the location of the child.
Now every time I set the instantiate angle to either the parent or the child, the projectiles seem to instantiate at double the angle I would expect. ie if the parent is travelling along the path at a 90-degree angle to it's starting direction, it will fire perpendicular to the current direction that it is facing.
This would make sense to me, in that, it may be trying to: take the angle the player has moved, say 45-degree, and then will set the angle to 45-degrees from the direction the player is now facing. But I have tried several different setups with the code to avoid this, and some different Quaternion functions. But the end result seems to be the same. I also tested whether or not radians/degrees were getting in the wrong way (which I assumed wouldn't but as I was at a loss, I tried it anyway).
I have a basic understanding of C#, Unity and Quaternions, as evident by the fact that this is a uni assessment. But I haven't been able to find, online, or in the documentation, the reason for this instantiation to not work properly.
Once I get this sorted then I can move onto the next part. I need to fire 2 projectiles at once, each at a slightly different angle. However this can't be done until I can actually fire in a straight line.
As a side note, the projectile prefab has a script attached which propels it forward, and that is it, no turning, no rotation, just forward movement.
.
Quaternion bull;
GameObject justFired;
bull = transform.rotation;
bull.y = bull.y - 0;
justFired = Instantiate(bullets, _thePlayer.transform.position, new Quaternion(0, bull.y, 0, 1)) as GameObject;
That is what is plugged in at the moment. I will quickly add in how I had it set up initially, how I would imagine that I should do it:
justFired = Instantiate(bullets, _thePlayer.transform.position, this.transform.rotation) as GameObject;
.
I just need to know how I have set up the instantiate incorrectly. It literally looks, when I test it, that it is instantiating at 2x the angle that it should. I will probably look so stupid after all this, but I am blind to my problem right now, despite how simple it probably is :s
Cheers for the help!
Answer by Dave-Hampson · Apr 04, 2014 at 11:25 AM
You seem to be using Quaternions as if they are Euler angles, which is not going to work. new Quaternion(0, bull.y, 0, 1) is not only not the rotation you want, it is not even a valid Unit Quaternion, unless bull.y is equal to 0.
If you use bull = transform.rotation; Instantiate(bullets, _thePlayer.transform.position, bull ) it should work.
Note: if you do want to do calculations with rotations using Euler angles, you can convert to Euler form and back to a Quaternion again.
Ah okay, Quaternions are something I am still developing knowledge on. I know I am not really meant to mess with them unless I know them well, but I thought I was more on top of it.
This is a big amount of help, thanking you. I won't be testing anything straight away, as I have limited time and a few other things I need to do. But there is a later assessment that is a build on, on-top of this, so when I start working on that I would drop back in and let you know how I go with this new info.
Thanks again for replying, and replying so quick. Cheers!