- Home /
How to prevent a spawn if my ray is on a certain object?
Here is my code: using UnityEngine; using System.Collections;
public class Instantiate : MonoBehaviour {
//position and rotation can be equal to that of an object if you use the object's ID.
Ray ray;
RaycastHit hit;
public GameObject Sphere;
void Update ()
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit))
{
if (Input.GetMouseButtonDown (0))
{
Instantiate (Sphere, hit.point + new Vector3(0, 0.5f, 0), Quaternion.identity);
}
}
}
}
How can I make it so that when the ray hits a sphere clone, the instantiation code does not execute? Keep in mind that I am new to Unity, so try to use simple terms on me.
Answer by Mr_Edward · Oct 06, 2015 at 06:59 PM
It's actually really simple: All you have to do is to test if your raycast is hitting the sphere clone. We can do this simply by checking if the name of the object we've hit is "Sphere (clone):
public class Instantiate : MonoBehaviour {
//position and rotation can be equal to that of an object if you use the object's ID.
Ray ray;
RaycastHit hit;
public GameObject Sphere;
void Update ()
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit))
{
if (Input.GetMouseButtonDown (0))
{
if(hit.transform.name == "Sphere (Clone)")
{
Instantiate (Sphere, hit.point + new Vector3(0, 0.5f, 0), Quaternion.identity);
}
}
}
}
}
Although this would be good for when I want something to clone at a sphere, I actually do not want it to clone at all. I just want to say this basically
If: I click at a sphere or one of its many clones.
Then: No sphere is made.
Your answer
Follow this Question
Related Questions
How can one rotate an object instantly? 2 Answers
Card Game like yu-gi-oh 1 Answer
First Person Controller - Problems with footstep audio being played too fast. 0 Answers
Iterate through area descibed by objects 1 Answer
MonoType Behavior 0 Answers