Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by Trollvahkiin · Aug 16, 2014 at 11:46 PM · instantiatequaternionspawn

Spawn object in front of player and the way he is facing?

Hey, how could I instantiate an object in front of the player AND the way he is facing? I've got this but it only spawns the object X amount units from the player on the x axis.

 playerPos = player.transform.position;
 spawnPos = new Vector3(playerPos.x+10, playerPos.y, playerPos.z);
 
 Instantiate(Resources.Load(iconDragging.GetComponent<UISprite>().spriteName), spawnPos, Quaternion.identity);
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

2 Replies

· Add your reply
  • Sort: 
avatar image
18
Best Answer

Answer by PlasmaByte · Aug 16, 2014 at 11:51 PM

You can use the player.transform.forward direction to get the players direction as a vector and transform.rotation to get the orientation. You can also do math operations of vectors which makes things so much neater than modifying the components.

 Vector3 playerPos = player.transform.position;
 Vector3 playerDirection = player.transform.forward;
 Quaternion playerRotation = player.transform.rotation;
 float spawnDistance = 10;
 
 Vector3 spawnPos = playerPos + playerDirection*spawnDistance;
 
 Instantiate(Resources.Load(iconDragging.GetComponent<UISprite>().spriteName), spawnPos, playerRotation );
Comment
Add comment · Show 3 · 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 Trollvahkiin · Aug 17, 2014 at 12:00 AM 0
Share

Works like a charm! Thanks :D

avatar image adamgffrd · Dec 03, 2018 at 06:57 PM 0
Share

I have been struggling with creating a dash controller to move forward based on the camera's position along with the camera's forward facing direction and this has helped me alot. Thanks!

avatar image drewjosh · Jun 09, 2021 at 07:23 PM 0
Share

Amazing! Many thanks!

avatar image
0

Answer by Tassos24 · Jun 15, 2018 at 01:08 PM

One quick question on that. the spawnPos must get new values in Update() function I suppose? not at Start()? or in both of them?

Comment
Add comment · 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 zkerr577 · Nov 07, 2019 at 08:14 PM 0
Share

You could initiate spawnPos in Start(), but the only advantage I see would be to allow the desired behavior, here shooting, on the very first frame. I don't think it would hurt either way.

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet Spawning Problem 1 Answer

Rotating a certain axis offsets the other ones? 1 Answer

How do i prevent an object instantiating another object straight away 2 Answers

Quaternion.identity problem at random spawn 1 Answer

Spawning objects on trigger does not work as expected 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