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
1
Question by juzajoke · Nov 23, 2012 at 08:00 PM · camerainstantiatefps

How to instantiate a prefab in front of FPS camera?

Hi, currently building a project that required a cube or block to be spawn in front of the player like the Minecraft thing using FPS camera. Now I only able to spawn exactly the position of the camera which the player will stuck in the middle of the cube, I wanted it to be spawn slightly in front of where the camera is facing. Thank you.

 var creation : Transform;
 
 function Update () {
     if(Input.GetButtonDown("Fire1"))
         Instantiate(creation, Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation);
 }
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

3 Replies

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

Answer by Eric5h5 · Nov 23, 2012 at 10:12 PM

Use transform.forward, where "distance" is the desired number of units in front:

 Instantiate(creation, transform.position + transform.forward*distance, transform.rotation);
Comment
Add comment · Show 2 · 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 greatestprez · Nov 23, 2012 at 10:47 PM 0
Share

Or do that..way simpler :P

avatar image juzajoke · Nov 24, 2012 at 05:14 AM 0
Share

Thanks, it working now and its really simple :D

avatar image
0

Answer by greatestprez · Nov 23, 2012 at 10:09 PM

Create a new empty game object, name it "pos", make it a child object of your main camera, reset it's position, and then set its z-position to 3. (make sure you do steps in this order) Then alter your script to say this:

 var creation : Transform;
 
 function Update () {
     if(Input.GetButtonDown("Fire1"))
        Instantiate(creation, transform.FindChild("pos").position, transform.FindChild("pos").rotation);
 }
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 juzajoke · Nov 24, 2012 at 05:14 AM 0
Share

Thanks, it working now

avatar image
0

Answer by laradov · Nov 24, 2012 at 12:33 AM

Try this:

     private GameObject objCamera;
     private Vector3 SpawnPosition;
     private int DistanceToCamera = 5;
     public Transform creation;
     
     void Start () {
         objCamera = (GameObject) GameObject.FindWithTag("MainCamera");
     }
     
     void Update () {
         if(Input.GetButtonDown("Fire1")) {
             SpawnPosition = objCamera.transform.forward * DistanceToCamera + objCamera.transform.position ;
             Instantiate(creation, SpawnPosition, Quaternion.identity);
         }    
     }
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 juzajoke · Nov 24, 2012 at 05:15 AM 0
Share

thanks, it working now :)

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

13 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

Related Questions

Which is better, shooting a bullet from camera or from the gun itself? 3 Answers

Checking if object intersects? 1 Answer

Multiplying GUIs 0 Answers

Lerping Field Of View is buggy 1 Answer

Projectile instantiation positioning is off. 3 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