I have imported a surface which I modeled with ‘blender’, I imported it with mesh collider. When I want to do a ray casting on this surface to place random objects it won’t work. When I set a standard plane above this surface, the ray casting works on this plane. So I think my modeled surface isn’t catching the ray casting, but I don’t know why. Can anybody help me?
using UnityEngine;
using System.Collections;
public class RandomlyPlaceObjectsOnSurface : MonoBehaviour
{
public GameObject[] objectsToSpawn;
public float spawnRadius = 10.0f;
public int numberOfObjects = 10;
// Use this for initialization
void Start ()
{
for(int i = 0; i < numberOfObjects; i++)
{
//What we will spawn
GameObject objectToSpawn = objectsToSpawn[Random.Range(0,objectsToSpawn.Length)];
Vector2 spawnPositionV2 = Random.insideUnitCircle*spawnRadius;
Vector3 spawnPosition = new Vector3(spawnPositionV2.x,0.0f,spawnPositionV2.y);
Vector3 transformOffsetSpawnPosition = transform.position+spawnPosition;
RaycastHit hit;
if (Physics.Raycast (transformOffsetSpawnPosition, Vector3.down, out hit)) {
Vector3 finalSpanPosition = hit.point;
Instantiate (objectToSpawn, finalSpanPosition, Quaternion.identity);
}
}
}
}
This is my code.