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 Fabio Prevedelli · Oct 04, 2010 at 10:17 AM · collidertransformtrigger

Check if another object exist in a position before respawning

Greetings!

Short problem description: I have an object that i'd like to respawn on my gaming board but do so in an "empty" place

Detailed description of my setup: I have a 2d chessboard like background (z=0 always), in this prototype the motion is done through scripting and the position x,y are always integers. I have three object type: Player (which is a cube atm of scale 1) , Many Obstacles (of scale 1) and a Fruit to pickup. Each of the object occupy a cell in my gaming board.

When the player enter the same cell as the fruit stuff happens and the Fruit respawn elsewhere on the board. What i'd like to do is having the fruit respawn in an empty cell not occupied by the player and by all the other obstacles.

Obstacles and Players have cube collider "is trigger" and rigid bodies.

In the Player Script i have

void OnTriggerEnter(Collider otherObject) {

 if (otherObject.gameObject.tag == "Food")
 {
     //Debug.Log("We hit: " + otherObject.name);
     Food food = (Food)otherObject.gameObject.GetComponent("Food");
     food.ResetPosition();

 }   

}

and the function to reset position in the Food is

public void ResetPosition()
{
transform.position = new Vector3(Mathf.RoundToInt(Random.Range(-gameref.backx, gameref.backx)), Mathf.RoundToInt(Random.Range(-gameref.backy, gameref.backy)), gameref.transform.position.z);
}

What i'd like to add is do a check that the coordinate i'm resetting the position are "Empty", i already tried to play around with a bruteforce approach like putting an ontriggerstay and reset the position again but this doesnt seem to work for me. Any help is appreciated :) as you can see i'm using c#

Thank you! Fabio

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

Answer by Albert · Oct 04, 2010 at 10:41 AM

since the positions are just integers, i think it isnt hard to compare positions, you can tag all objects that moves inside the chessboard, and compare all those objects to the current moving piece or the fruit you're talking about before instantiating it.

maybe something like this might work:

if(checkIfPosEmpty(targetPosition)) { Instantiate(fruit, targetPosition, Quaternion.identity); }

public bool checkIfPosEmpty(Vector3 targetPos) { GameObject[] allMovableThings = GameObject.FindGameObjectsWithTag("movable"); foreach(GameObject current in allMovableThing) { if(current.position == targetPos) return false; } return true; }

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 Fabio Prevedelli · Oct 04, 2010 at 10:58 AM 0
Share

Clave you seem to be my personal trainer =) I will try to adapt this to my code and see if it works =) The first doubt that comes to $$anonymous$$d is if i can attach multiple tags to my objects (thus adding your movable tag) since i need to avoid both players and obstacles which are two different things.

avatar image Albert · Oct 04, 2010 at 04:53 PM 0
Share

if you have multiple tags, maybe you could make two GameObject arrays and two foreach, if the Obstacles are static per game session, you could save their position in an array too on the start of the game, so you would never need to FindGameObjectWithTag on them again and again :)

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

No one has followed this question yet.

Related Questions

Get the colliders transform (?) 1 Answer

How Do I Create Elastic Ropes For a Wrestling Ring? 1 Answer

My Y position goes fixed to some value 0 Answers

A node in a childnode? 1 Answer

Destory/Collect object to open door 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