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 /
This question was closed Jan 21, 2017 at 01:23 PM by Kernivorous for the following reason:

I fixed the problem.

avatar image
0
Question by Kernivorous · Jan 19, 2017 at 04:35 PM · 2d gamerayraycasthit2droguelikerandom gen

Problem with random room gen using raycasts!

My raycasts aren't hitting any colliders. There are four raycasts (top, left, right and bottom) that are meant to check if there is a room there and if there isn't, it can generate a room there. It doesn't seem to detect any rooms though so rooms are spawned on top of other rooms sometimes.

 public class RoomGeneration : MonoBehaviour {
 
  public int roomsGenerated; // How many rooms are generated
  public GameObject[] rooms;
  public GameObject doorTop;
  public GameObject doorBottom;
  public GameObject doorLeft;
  public GameObject doorRight;
  public GameObject rayUp;
  public GameObject rayDown;
  public GameObject rayLeft;
  public GameObject rayRight;
  int genX; // Room X coord
  int genY; // Room Y coord
  // Use this for initialization
  void Start()
  {
      genX = 0;
      genY = 0;
      Instantiate(rooms[0], new Vector2(genX, genY), Quaternion.identity);
      for (int i = 0; i <= 20; i++)
      {
          CheckAndBuild();
      }
  }
  // Update is called once per frame
  void Update()
  {
     
  }
  void CheckAndBuild()
  {
      int randomRoomDir = Random.Range(0, 4);
      int randomRoom = Random.Range(1, 6);
      RaycastHit2D hitUp = Physics2D.Raycast(rayUp.transform.position, transform.up, 3f);
      RaycastHit2D hitDown = Physics2D.Raycast(rayDown.transform.position, transform.up * -1, 3f);
      RaycastHit2D hitLeft = Physics2D.Raycast(rayLeft.transform.position, transform.right * -1, 5f);
      RaycastHit2D hitRight = Physics2D.Raycast(rayRight.transform.position, transform.right, 5f);
      Debug.DrawRay(rayUp.transform.position, transform.up * 3f, Color.red, 1000f);
      Debug.DrawRay(rayDown.transform.position, transform.up * -1 * 3f, Color.red, 1000f);
      Debug.DrawRay(rayLeft.transform.position, transform.right * -1 * 5f, Color.red, 1000f);
      Debug.DrawRay(rayRight.transform.position, transform.right * 5f, Color.red, 1000f);
      if (randomRoomDir == 0 && hitUp.collider == null) // Generate room above
      {
          Instantiate(doorTop, new Vector2(genX, genY + 3.55f), Quaternion.identity);
          genY += 10;
          Vector2 roomGenPos = new Vector2(genX, genY);
          transform.position = roomGenPos;
          Instantiate(rooms[randomRoom], roomGenPos, Quaternion.identity);
          Instantiate(doorBottom, new Vector2(genX, genY - 3.55f), Quaternion.identity);
          print(hitUp.collider);
      }
      if (randomRoomDir == 1 && hitDown.collider == null) // Generate room below
      {
          Instantiate(doorBottom, new Vector2(genX, genY - 3.55f), Quaternion.identity);
          genY -= 10;
          Vector2 roomGenPos = new Vector2(genX, genY);
          transform.position = roomGenPos;
          Instantiate(rooms[randomRoom], roomGenPos, Quaternion.identity);
          Instantiate(doorTop, new Vector2(genX, genY + 3.55f), Quaternion.identity);
          print(hitUp.collider);
      }
      if (randomRoomDir == 2 && hitLeft.collider == null) // Generate room to the left
      {
          Instantiate(doorLeft, new Vector2(genX - 7.4f, genY), Quaternion.identity);
          genX -= 20;
          Vector2 roomGenPos = new Vector2(genX, genY);
          transform.position = roomGenPos;
          Instantiate(rooms[randomRoom], roomGenPos, Quaternion.identity);
          Instantiate(doorRight, new Vector2(genX + 7.4f, genY), Quaternion.identity);
          print(hitUp.collider);
      }
      if (randomRoomDir == 3 && hitRight.collider == null) // Generate room to the right
      {
          Instantiate(doorRight, new Vector2(genX + 7.4f, genY), Quaternion.identity);
          genX += 20;
          Instantiate(rooms[randomRoom], new Vector2(genX, genY), Quaternion.identity);
          Instantiate(doorLeft, new Vector2(genX - 7.4f, genY), Quaternion.identity);
          print(hitUp.collider);
      }
  }
 }

Any help would be greatly appreciated! This is driving me insane.

alt text

In the picture you can see the drawlines that show what the rays are meant to be doing. The rays come from empty gameobjects that are children of another gameobject which is what this script is attached to. When a room is generated, this object moves to the center of the new room at the same time so the raycasts are always in the right place. The raycasts just wont detect anything they collide with.

Also, the Debug.DrawLine's don't all fire properly. You can't see it in the picture but sometime they don't appear.

unity-help-pic.jpg (248.2 kB)
Comment
Add comment · Show 1
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 Kernivorous · Jan 19, 2017 at 04:15 PM 0
Share

I have fixed where it says print(hitUp.collider); in the wrong places.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why aren't raycasts hitting any colliders? 0 Answers

Why does this code run infinitely? 1 Answer

How can i detect 2 raycasthit2D in the same function storing their Vector2 position? 0 Answers

Please help with raycast2d 0 Answers

How can I create a bullet hell roguelike game? 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