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 skate4life13010 · Nov 12, 2014 at 10:25 AM · instantiatejavaoverlapscipt

GameObjects being instantiated on top of each other

I want my character to move and place cubes behind itself as it walks. When I step on a cube that already exists another one is instantiated on top of it. How do I go about changing this?

 //#pragma strict
 var endCube = gameObject;
 var prefab = gameObject;
 
 var collide : boolean = false;
 
 function OnCollisionEnter(coll: Collision){   //Colliding with object?
         if(coll.gameObject.tag=="floor"){
             collide=true;    
             Debug.Log("collision On");
             }
         else{
             collide=false;
                 Debug.Log("collision off");
         }
 }
 function OnCollisionExit(coll: Collision){
             collide=false;            
             Debug.Log("Stay");
 }
 
 
 function Update () {
 myPosition = transform.position;
 
     if(Input.GetButtonDown("up")){
         transform.position.y = transform.position.y+1;
         Instantiate(prefab, transform.position, Quaternion.identity);
     }
     if(Input.GetButtonDown("down")){
         transform.position.y = transform.position.y-1;        
         Instantiate(prefab, transform.position, Quaternion.identity);
     }
     if(Input.GetButtonDown("left")){
         transform.position.x = transform.position.x-1; 
         Instantiate(prefab, transform.position, Quaternion.identity);
     }
     if(Input.GetButtonDown("right")){
         transform.position.x = transform.position.x+1;
         Instantiate(prefab, transform.position, Quaternion.identity);
     }
     if (Input.GetKeyDown (KeyCode.Space)){
         Instantiate(endCube, transform.position, Quaternion.identity);
     }
     
     Debug.Log(myPosition);    
 /*    if (collide == false){
         Instantiate(prefab, transform.position, Quaternion.identity);
             Debug.Log("collide = false");*/
     
 }
 
     
 
     

Comment
Add comment · Show 2
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 richyrich · Nov 13, 2014 at 01:06 AM 0
Share

If each of your objects have trigger colliders, then you could set bool cubesNearby = true; during OnTriggerEnter/Stay/Exit. Altneratively, as you instantiate, record the objects in an array, then check through the array looking for the distance (Vector3.Distance) between the player and the cube - if any cube too close, then set a bool cubesNearby = true, that way.

avatar image WilliamLeu · Nov 13, 2014 at 04:48 PM 0
Share

Query the scene with a SphereCast and see if something already exists there before creating a cube.

Documentation links > CheckSphere, SphereCast, SphereCastAll

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Serinx · Nov 13, 2014 at 02:36 AM

Similar to @richyrich's idea, you could have a list of Vector3's that store the positions of the cubes you have already placed:

 private List<Vector3> BlockPositions = new List<Vector3>;
 

Before moving and placing a block you could check to see if the position already exists. If it doesn't then move and place the block and also add the new position to the list.

 if(Input.GetButtonDown("up")){
          transform.position.y+=1;
          If (!BlockPositions.Exists(transform.position))
          {
              //Path is clear, move the player, instantiate a cube and add the position to the list
              Instantiate(prefab, transform.position, Quaternion.identity);
              BlockPositions.Add(transform.position);
          }
          else
          {
              //Path is not clear, move the player back
              transform.position.y-=1;
          }
      }

Not in a position to test the code but you get the general idea.

Note: This method would require that your character/blocks are always in the exact position!

Good luck!

Comment
Add comment · 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
0

Answer by richyrich · Nov 13, 2014 at 10:39 AM

 If (!BlockPositions.Exists(transform.position))

1) Assumes that each block is no more than 1 unit in height - maybe it is

2) Assumes that the player position starts from and can only exist in whole numbers rather than floats (Vector3 = float, float, float) this seems less likely

   {
       //Path is not clear, move the player back
       transform.position.y-=1;
   }

3) This now means that once you have laid one cube in a location, the character is frozen

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 Serinx · Nov 13, 2014 at 07:19 PM 0
Share

Yes I did make those assumptions based on the fact that the character moves 1 unit when the button is pressed, moving the player back was just a roundabout way of not letting him move where blocks are already placed. If this assumption was incorrect then he can remove this part :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Tags Help scripting 1 Answer

Web Build Loading Slow 1 Answer

Can anyone help check for colliders in this spawning script so that the spawns don't overlap each other or spawn inside of walls, objetcs etc 0 Answers

I need help with rotation 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