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 /
avatar image
0
Question by Chocolade · Feb 21, 2017 at 09:18 AM · scripting problemscript.sceneinvisiblewalls

How can i use a script to automatic place invisible walls around the terrain edges ?

For now i did it on my own. Created 4 empty gameobjects added box collider to each one of them and then dragged them with the mouse to the terrain edges. But they are not in the exactly places i mean not exactly touching the edges of the terrain.

Is there a way to calculate with a script and find the edges of the terrain positions and place the walls automatic to fit each edge ?

Invisible Walls

ss57.jpg (266.7 kB)
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 MostNimbus · Feb 21, 2017 at 09:45 AM 0
Share

I assume that your map isn't a square, right?

avatar image RobAnthem · Feb 21, 2017 at 10:20 AM 0
Share

You can easily create empty game objects via code, add colliders, define the space of the collider, and define its location. Basically everything you just did, but in code. That should be easy for you if you are familiar with C# and Unitys API. As far as the terrain size goes, you can reference the terrain object, and the terrain data contains the information about the terrain.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Patxiku · Feb 21, 2017 at 02:29 PM

You could either use the V key to align the GameObjects to the terrain in the editor mode or try instatiating new GameObjects based on the terrain position and length in runtime. I've created a simple script that will do it, the terrain doesn't need to have same width and length to make this work:

 public class TerrainBorderGenerator : MonoBehaviour {
     //Attach this script to the terrain containing GameObject
     void Start () {
         //Get the terrain position and size
         Vector3 terrainPosition = transform.position;
         Vector3 terrainSize = GetComponent<Terrain>().terrainData.size;
         //Call the insantiating function
         InstantiateEdgeWidthBorders(terrainPosition, terrainSize);
         InstantiateEdgeLengthBorders(terrainPosition, terrainSize);
     }
     //Since our borders size will be the same, we divide the borders in two cases: Width and Length
     private void InstantiateEdgeWidthBorders(Vector3 terrainPosition, Vector3 terrainSize){
         //We calculate the width offset for our borders 
         //Since our left border will be in the same position as the terrain X position, we only need to calculate the right border offset 
         float zOffset = terrainSize.z;
         //We also need to calculate the X offset of the terrain (wich is the half of it size) to center the borders to the terrain
         float xOffset = terrainSize.x / 2;
         //Since Width borders share same scale... 
         for (int i = 0; i < 2; i++){
             GameObject border = GameObject.CreatePrimitive(PrimitiveType.Cube);
             //As we don't care of the length, we assign 1 in the Z scale
             border.transform.localScale = new Vector3(terrainSize.x,terrainSize.y,1);
             //Now we need to calculate the position of our borders based on our terrain position , we will asume that i = 0 is the left border and i = 1 the right one
             if (i == 0)
                 border.transform.position = new Vector3(terrainPosition.x + xOffset, terrainPosition.y, terrainPosition.z);
             else
                 border.transform.position = new Vector3(terrainPosition.x + xOffset, terrainPosition.y, terrainPosition.z + zOffset);
             //We disable our mesh renderer
             DisableMeshRenderer(border);
             //For clean scene purposes, we will make our borders childs of the terrain and assign them a clear name
             border.transform.name = "Width Border " + (i + 1);
             border.transform.parent = transform;
         }
     } 
     //Same procedure goes for the length borders
     private void InstantiateEdgeLengthBorders(Vector3 terrainPosition, Vector3 terrainSize){
         //Since we are looking for length, we need to change these offsets
         float xOffset = terrainSize.x;
         float zOffset = terrainSize.z / 2;
         for (int i = 0; i < 2; i++)
         {
             GameObject border = GameObject.CreatePrimitive(PrimitiveType.Cube);
             //We don't care about the width, so we assign 1 into the X axis
             border.transform.localScale = new Vector3(1, terrainSize.y, terrainSize.z);
             if (i == 0)
                 border.transform.position = new Vector3(terrainPosition.x, terrainPosition.y, terrainPosition.z + zOffset);
             else
                 border.transform.position = new Vector3(terrainPosition.x + xOffset, terrainPosition.y, terrainPosition.z + zOffset);
             DisableMeshRenderer(border);
             border.transform.name = "Length Border " + (i + 1);
             border.transform.parent = transform;
         }
     }
     //We don't want our borders to be seen, do we?
     private void DisableMeshRenderer(GameObject border){
         border.GetComponent<MeshRenderer>().enabled = false;
     }
 }


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

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

94 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Whats the best method to change a script in one scene, without changing the same script in a different scene. 1 Answer

The script doesn't work correctly 0 Answers

How do I build up speed while wallrunning? 1 Answer

(PLEASE HELP) hi guys! i have a problem with my Score text 1 Answer

Saving objects through scenes 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