- Home /
Collider for rocks.
Hi there. Sorry for that my question may be stupid, but I'm a novice to Unity (also sorry for bad english). I'm now making my first scene. It is a forest. And as I added trees by painting them, I made tree colliders by creating prefab with tree model, adding capsule collider to it and replacing the original tree object with my prefab in terrain tree-painting menu. But what about rocks? If I paint them as terrain details (like flowers, grass etc.), they will look different as I exactly want, but will have no colliders. Tried to paint them as trees (with creating a prefab and adding collider to it), but they still had no collider and also look similar. Of course i can add a collider to every place where I have a rock, but I've got like 50-60 rocks in my forest. Any solutions?
Select all rocks in the scene by selecting them in your hierarchy view. Then In your inspector view click add component, select physics, select mesh collider. There are many options in mesh collider when you add it. See what suit your needs.
Red.
Bored$$anonymous$$ormon, as i wrote in post i tried that, but not working.
Redeemer86, they are not exist in hierarchy as i painted them by terrain tools. Adding rocks one by one as objects will make them look simiral.
Answer by bubzy · Oct 10, 2014 at 09:24 AM
I use something like this, its not a painter, and I'm not entirely sure how the painter works, but this will randomly populate a set area with rocks (drag the model including collider into the "item" field in the inspector and enter the other properties too to customise the item spawns
using UnityEngine;
using System.Collections;
public class itemspawner : MonoBehaviour {
// Use this for initialization
public Transform item;
public Vector2 spawnAreaBottomLeft = new Vector2(0,0);
public Vector2 spawnArea = new Vector2(100,100);
public int spawnQuantity = 10;
public bool randomSize = false;
public bool randomRotation = false;
public bool randomRotationY = false;
public float offsetY = 0f;
//public bool dominant = false;
Vector2 bLeft; //quicker! :D
public float exclusionZone = 10f;
//int i = 0;
public string priorityTag; //when this item is spawned, delete it if it collides with the priorityTag item
public void spawnItems()
{
bLeft = spawnAreaBottomLeft;
for(int i = 0; i < spawnQuantity; i++)
{
Transform temp;
Vector3 tempPos = new Vector3(Random.Range(bLeft.x,bLeft.x+spawnArea.x),0,Random.Range(bLeft.y,bLeft.y+spawnArea.y));
temp = Instantiate(item,tempPos+new Vector3(0,Terrain.activeTerrain.SampleHeight(tempPos)+offsetY,0),Quaternion.identity) as Transform;
if(randomSize)
{
float _randomSize = (float)Random.Range(1,10)/10;
temp.transform.localScale = new Vector3(_randomSize,_randomSize,_randomSize);
}
if(randomRotationY)
{
//Quaternion tempRot = new Quaternion(0f,Random.Range(0f,360f),0f,0f);
temp.transform.RotateAround(new Vector3(0,1,0),Random.Range(0,360));
}
if(randomRotation)
{
temp.transform.localRotation = Random.rotation;
}
temp.parent = gameObject.transform;
}
}
void Start () {
}
// Update is called once per frame
void Update () {
}
}
hope this helps
Thanks, but I dont understand a few things. 1.Should I add this script to my terrain? Or to my rock prefab? 2.It will spawn rocks immediately, or how I'm gonna make it start?
im not being nasty or preachy here, but you should really follow a few tutorials first.
you can attach this to an empty and add the following line in the void Start()
function
void Start()
{
spawnItems();
}
the way I did it was to have a separate scene manager object that referenced and called all of the spawn functions remotely, this is why the function is public
Your answer
![](https://koobas.hobune.stream/wayback/20220613164730im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Find right mesh for collider 1 Answer
Box Collider on Trees Are Not Rotating When Painted to Terrain 1 Answer
Character keeps falling through terrain (with colliders) 4 Answers
Terrain Detail Mesh Collisions 1 Answer
Technique to find a mesh intersecting with a primitive solid without using colliders? 0 Answers