- Home /
Use raycast to see if something else than terrain is under
Hi! I have a script that instantiates an object that I need to not instantiate if there already is something else than terrain where I instantiates. The most logical solution I think would be something using a raycast, although I'm not certain how I would check if there is something else than terrain where I instantiates...
Here is the instantiation code
if(Input.GetKeyDown(KeyCode.Mouse0)){
Debug.Log ("Instantiating Windmill.");
GameObject newObject;
newObject = (GameObject) Instantiate(Object1, hit.point, Quaternion.Euler (0,120,0));
Vector3 currentPosition = newObject.transform.position;
currentPosition.y = currentPosition.y + 500.0f;
newObject.transform.position = currentPosition;
}
Thanks in advance!
Answer by whydoidoit · Oct 06, 2013 at 02:59 PM
So best would be to use a SphereCast from above the position you want heading downwards. If it hits something check whether it is the terrain by considering the RaycastHit collider's tag or by using it to get the game object. If it isn't the terrain then something else is there.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Making a particle effect parallel to the slope of a terrain 1 Answer
Make an instantiated object match the slope of the terrain 1 Answer
Find a random location, spawn an object there, do it again. 0 Answers
Quickest most efficient way of instantiating a large amount of GameObjects 1 Answer