- First, go to the object that you want the ray to check if that's the object it hit
- When you have that object selected, look in the inspector at the very top
- Where it says tag, click on the "untagged" and a list should drop
- At the very bottom of that list, click on "Add tag"
- Expand tags and increase the "Size" by one
- Once you increased the size , a new element should of been added
- Click on that element and give it the name you want, let's say you called it "target"
- Go back to your object and on the tag drop down choose the name you gave the element
Now we can do this:
public class LeftClick : MonoBehaviour
{
void Update()
{
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100))
{
if(hit.collider.gameObject.tag == "target")//substitute "target" for your tag name
Application.LoadLevel("Barracks");
}
}
}
So what that does is, it checks to see if whatever the ray hit's tag is target. If you assigned the tag "target" to a gameObject; when the ray hits it, it'll execute the code.
↧