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 javert · Dec 09, 2013 at 03:36 AM · prefabpositionspawn

How can I spawn a prefab relative to a cube position?

Good evening everyone, I can not spawn a prefab relative to a cube position, I want to spawn the prefab in position Z+1. My Script in C# my prefab is "public GameObject seta1;"

 public class regraBlocos : MonoBehaviour {
 
  
 
 public Vector3 selfPosition;
 
 private bool blocoAtivo = true;
 
 public bool blocoPressionado = false;
 
 public GameObject seta1;
 
 public GameObject seta2;
 
 public GameObject seta3;
 
 public GameObject seta4;
 
 public Vector3 spawnSpot1;
 
 public Vector3 spawnSpot2 = new Vector3(0, 0, 0);
 
 public Vector3 spawnSpot3 = new Vector3(0, 0, 0);
 
 public Vector3 spawnSpot4 = new Vector3(0, 0, 0);
 
 private int pushing = 0;
 
 // Use this for initialization
 
 void Start () {
 
 selfPosition = transform.localPosition;
 
 }
 
 void SpawnSetas(){// regra para spawnar somente uma vez
 
 if (pushing == 0)   {
 
 Instantiate
 
  
 
 (seta1,transform.localPosition, Quaternion.identity);
 
  
 
 }
 
 pushing = 1;
 
 }
 
  
 
  
 
 // Update is called once per frame
 
 void Update()
 
  
 
 {
 
 if (Input.GetMouseButtonDown(0) & blocoAtivo ==true)
 
 {
 
  
 
 RaycastHit hitInfo = new RaycastHit();
 
 bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input .mousePosition), out hitInfo);
 
 if (hit) 
 
 {
 
 if (hitInfo.transform.gameObject.tag == "BLOCO" & blocoAtivo ==true)
 
 {blocoPressionado = true;
 
 SpawnSetas();
 
 } 
 
 //regra se o ray colidir com as tags o bloco se move
 
 if (hitInfo.transform.gameObject.tag == "CIMA" & blocoAtivo ==true)
 
 {transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1.0f);
 
 blocoAtivo = false;} 
 
 if (hitInfo.transform.gameObject.tag == "DIREITA" & blocoAtivo ==true)
 
 {transform.position = new Vector3(transform.position.x + 1.0f, transform.position.y, transform.position.z);
 
 blocoAtivo = false;} 
 
 if (hitInfo.transform.gameObject.tag == "BAIXO" & blocoAtivo ==true)
 
 {transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1.0f);
 
 blocoAtivo = false;} 
 
 if (hitInfo.transform.gameObject.tag == "ESQUERDA" & blocoAtivo ==true)
 
 {transform.position = new Vector3(transform.position.x - 1.0f, transform.position.y, transform.position.z );
 
 blocoAtivo = false;} 
 
  
 
 }
 
 }
 
 }
 
 }
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

Answer by aldonaletto · Dec 09, 2013 at 03:47 AM

Don't use localPosition: it's just the object position relative to its parent (if there's no parent, it's the same as transform.position). You should use transform.TransformPoint instead:

 Instantiate(seta1,transform.TransformPoint(Vector3.forward), Quaternion.identity);

TransformPoint converts the Vector3 passed from local to world space.

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 javert · Dec 09, 2013 at 10:04 AM 0
Share

Doesn't works.![alt text][1]

inicio2.jpg (93.9 kB)

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

17 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

Related Questions

Setting Position of Spawned Prefab 2 Answers

The appearance of some prefab appears somewhere else but their collider stays in the good place, that to make? 0 Answers

GameObject position and localPosition not changing in hiearchy, only in script. 0 Answers

My prefabs always spawn twice at different positions 1 Answer

How do you instantiate just one prefab? 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