Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by lampshade · Dec 08, 2010 at 02:40 AM · instantiateprefabrotate

Instantiate in front of player (and give velocity)

Hi,

For some reason, I cannot instantiate in front of the player after I rotate him. How can I keep the players y rotation the same as when I instantiate a projectile?

the script that I am doing this in is attached to the player:

GameObject instPrefab = (GameObject) Instantiate(Prefab, transform.position, Quaternion.Euler(Input.mousePosition.y * factor, Input.mousePosition.x, 0));
instPrefab.rigidbody.velocity = instPrefab.transform.TransformDirection(-Vector3.forward * 200); // is not fully in front of the player
Comment
Add comment
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
3
Best Answer

Answer by Jesse Anders · Dec 08, 2010 at 06:02 AM

I didn't try to figure out everything that's going on there, but it looks like you're making the common mistake of trying to treat the elements of a quaternion as (Euler) angles. This, for example:

Quaternion(transform.rotation.x, transform.rotation.y, transform.rotation.z, 1)

Almost certainly doesn't do what you think it does, and is very unlikely to do anything useful.

In short, use Transform.rotation and .localRotation when you want to work with the orientation directly in quaternion form, and Transform.eulerAngles and .localEulerAngles when you want to work with the orientation in Euler-angle form.

Comment
Add comment · Show 4 · 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 lampshade · Dec 08, 2010 at 06:45 PM 0
Share

It seems that when I need to fire my projectile at an angle, I can't get the instantiation to always appear in front of the player.

avatar image Jesse Anders · Dec 08, 2010 at 10:09 PM 0
Share

Look at Transform.TransformPoint() (it will allow you to specify a point that's relative to the player's current position and orientation).

avatar image lampshade · Dec 09, 2010 at 07:03 AM 0
Share

I reposted with what almost works...still inaccurate

avatar image Jesse Anders · Dec 09, 2010 at 10:34 PM 0
Share

I can't really tell from your code what you're trying to do. (For one thing, to instantiate something in front of the player, you would typically offset the position relative to the player's position, but you appear to be using the player's position unmodified.)

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

rot not working for 2nd prefab 2 Answers

Associate objects to a prefab 1 Answer

instantiating vertically 2 Answers

Brain freeze: declaring a prefab inside the same prefab 1 Answer

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 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