• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Radact · Apr 01, 2014 at 09:22 AM · instantiatequaternionparentchildangle

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!

Comment
OllyNicholson

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

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.

Comment
Ben-Stoneman
Lo0NuhtiK
Radact

People who like this

3 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Radact · Apr 06, 2014 at 08:51 AM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Instantiating prefabs as child of a gameobject 1 Answer

can't parent an Object, a second time. 0 Answers

Instantiate prefab as Child to remove and re-do later 2 Answers

How to Instantiate Prefab as Child of Player OnTriggerEnter 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges