Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by JacobLadder · May 30, 2016 at 10:09 PM · physicscheckbox

physics.checkbox

Hi All,

I'm very new to c# but I'm muddling through slowly. I am try to make a game based on the old DeathChase game on the spectrum. Today you would call it an endless runner type of game. Your on a motorbike and you chase other bikes through a forest. Now my problem is this. I am trying to use Physics.CheckBox to check if my trees can spawn without them colliding with other trees. At the moment I am not getting much success as my trees overlap. Is there something wrong with the code or am I missing an element in the Unity Editor. All the trees have a rigidbody and a collider which the trigger is set to false. cheers in advance of any help.

using UnityEngine; using System.Collections;

public class HazardSpawner : MonoBehaviour {

 public GameObject[] hazards;

 public int maxHazardCount = 20;

 public int hazardCount = 0;

 private Transform thisTransform;

 private Vector3 thisColl;


 void Start () {
     
     thisTransform = GetComponent<Transform> ();

     thisColl = GetComponent<BoxCollider> ().size;
      
 }

 void Update(){
     
     if (hazardCount < maxHazardCount) {
         
         SpawnHazard ();
     }
 }

 void SpawnHazard(){
     
     float spawnPoint = thisTransform.position.x;

     int hazard = (int)Random.Range (0, hazards.Length);

     Vector3 spawn = new Vector3(Random.Range(spawnPoint + -5.0f, spawnPoint + 5.0f), thisTransform.position.y, Random.Range(thisTransform.position.z, thisTransform.position.z + 10.0f));

     bool isFreeOfObstacle = Physics.CheckBox (thisColl / 2, thisColl);

     if (isFreeOfObstacle) {

         Instantiate (hazards [hazard], spawn, Quaternion.identity);

         hazardCount++;
     }
 }

}enter code here

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 JacobLadder · May 31, 2016 at 06:31 AM 0
Share

Just found a stupid mistake I was doing. the script was attached to a game empty. and this had no box collider. I have sorted that out and I when I set the isFreeObstacle to true I still get trees overlapping. When set to false I only get one tree spawning..

avatar image JacobLadder · May 31, 2016 at 07:13 AM 0
Share

using UnityEngine; using System.Collections;

public class HazardSpawner : $$anonymous$$onoBehaviour {

 public GameObject[] hazards;

 public Transform emptySpawnPosition;

 public int maxHazardCount = 20;

 public int hazardCount = 0;

 private Transform thisTransform;

 private Vector3 emptySpawnCollider;


 void Start () {
     
     thisTransform = GetComponent<Transform> ();
      
 }

 void Update(){

     bool isFreeOfObstacle;

     if (hazardCount < maxHazardCount) {
         
         int hazard = (int)Random.Range (0, hazards.Length);

         float spawnPoint = thisTransform.position.x;

         Vector3 spawnPosition = new Vector3(Random.Range(spawnPoint + -20.0f, spawnPoint + 20.0f), thisTransform.position.y, Random.Range(thisTransform.position.z, thisTransform.position.z + 20.0f));

         Instantiate (emptySpawnPosition, spawnPosition, Quaternion.identity);

         emptySpawnCollider = emptySpawnPosition.GetComponent<BoxCollider> ().size;

         isFreeOfObstacle = Physics.CheckBox (emptySpawnPosition.position, emptySpawnCollider);

         if (!isFreeOfObstacle) {

             print ("False");

             GameObject thisSpawnedGameObject = (GameObject)Instantiate (hazards [hazard], spawnPosition, Quaternion.identity);

             hazardCount++;

         } else if (isFreeOfObstacle) {
             print ("True");
         }
     }
 }

}enter code here Where I am at now.. Still doesn't work.. Any ideas please.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Jun 01, 2016 at 07:44 AM

Rather than instantiate the tree then check you could get a random location and then check the closest tree. Tag the Tree prefab (e.g. Tree).

 private float closest;
 private GameObject[] TreeList;
 
 TreeList = GameObject.FindGameObjectsWithTag("Tree");
 
 closest = 1000f;
 foreach (GameObject aTree in TreeList)
 {
      float dist = Vector3.distance(spawnPosition, aTree.position);
      if (dist < closest)
           closest = dist;
 }

Then work out the minimum safe distance and if closest is less than that you have to pick another location.

That's coded straight from my head to the page so there may be the odd typo here or there but it might give you a hand getting started.

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 JacobLadder · Jun 01, 2016 at 09:14 PM 0
Share

Thank you for that. I will try and use what you have suggested. Being new to program$$anonymous$$g its difficult to know how to go about things. But your way seems very logical. Perhaps I'm over complicating a relatively simple function. Will post my efforts if successful. Once again thanks.

avatar image JacobLadder · Jun 03, 2016 at 04:33 AM 0
Share

I have tried to put your suggest into practice and it works in a fashion. The trees spawn but also they spawn in the same location as well. So there is three trees in the exact same place. Any suggests to stop this from happening.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Physics.CheckSphere isn't working well 0 Answers

Rigidbody is colliding terrain on isKinematic = false. call although meshes don't touch 0 Answers

Rigidbody rotation physics problem 1 Answer

How do I make pull object script out of this? 1 Answer

When putting up the friction it doesn't change anything. 0 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