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 Novrick · Oct 13, 2011 at 10:33 AM · instantiatepositionobjectsfirst-person-controller

FPS instantiate objects

I'm trying to make a game where you create objects when you press the mouse button.

I've followed the tutorial on 3Dbuzz and they helped me fix that prefabs spawn in the game everytime I press the mousebutton.

but the tutorial dealt with a simple 2D shooter, so the spawn position of the prefabs are pretty dull: they spawn in a fixed X position and the player is using the FPS script.

I want the objects to spawn where the player looks, both X and Y positions.

here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class playerscript : MonoBehaviour {
     
     public GameObject cubeprefab;
 //Vector3 originalposition;
 //Quaternion originalrotation;

 // Use this for initialization
 void Start () {
     
     //if(transform.position != null)
     //{
     //transform.position = originalposition;
     //transform.rotation = originalrotation;
     
     
     //}
 
 }
 
 // Update is called once per frame
 void Update () {
     
     
     if(Input.GetKeyDown("e"))
     {
     //originalposition = transform.position;
     //originalrotation = transform.rotation;    
     Application.LoadLevel("the cave");    
         
     }
     
     if(Input.GetKeyDown("mouse 1"))
     {
     Vector3 position = new Vector3(transform.position.x, transform.position.y + transform.localScale.y / 2);    
     Instantiate(cubeprefab, position, transform.rotation);    
         
     }
     
     
     
 
 }
 
 
 void Reset () {
     
 
 }
 
 

}

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
0
Best Answer

Answer by Proportion · Oct 13, 2011 at 11:09 AM

add a public variable gameobject named camera; then on your fps character thing add the childed camera gameobject to this public variable. then replace your mouse 1 code with this

if(Input.GetKeyDown("mouse 1")) {

 Instantiate(cubeprefab, camera.transform.position, camera.transform.rotation); 

 }

this will make your cube prefab pop straight out of where the camera is, unless your camera is in the player collider, in that case you have to use physics.ignorecollision for the cubeprefab and the fps thingy

Comment
Add comment · Show 6 · 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 Novrick · Oct 13, 2011 at 12:00 PM 0
Share

let me see if I got this right: I add the variable gameobject camera in the playerscript then I go to the unity interface and add the first person controller object to the gameobject variable in the playerscript directory?

avatar image Novrick · Oct 13, 2011 at 12:05 PM 0
Share

where do I write the ignorecollision? in the playerscript(which is in the first person controller) or do I fix a script for the cubeprefab?

avatar image Novrick · Oct 13, 2011 at 12:26 PM 0
Share

or is there a way to spawn the objects in front of the camera ins$$anonymous$$d? cuase if I make this right....the object will be created exactly where the player is(I'm making non moving cubes)

avatar image Proportion · Oct 14, 2011 at 12:44 AM 0
Share

you can spawn them infront of it but you have to make another empty gameobject for it to spawn from. just put physics.ignorecollision(cubeprefab.collider, collider) under your instantiated cube

avatar image Novrick · Oct 14, 2011 at 07:52 AM 0
Share

it works now, thanks alot man! : D

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiate control 1 Answer

Help, can't get Grid to instantiate in the center of the screen 0 Answers

Found if instantiate position already occupied 0 Answers

Spawning a prefab X units in 1 direction from an object 3 Answers

Position.z change to -20 when creating item 1 Answer


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