- Home /
How do I make it know what it's hitting?
using System;
using UnityEngine;
class LeftClick : MonoBehaviour
{
static void Click(){
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit, 100))
Application.LoadLevel("Barracks");
return;
}
}
}
}
This is what I have, but how do I make it know that it's gonna hit my GameObject called Main Frame? Do I need to insert a tag to this gameObject? a script to it? I'm new to this... Thanks!
Looking at your code and reading your question, I have no idea what you're asking. Can you try giving more detail?
I have an Object called $$anonymous$$ain Frame and I want to make it so that when I click it I load the level Barracks... however this isn't working because I need to Find the object, I tried plugging in GameObject.Find("$$anonymous$$ain Frame"), but I think I didnt use it right, so I just wanted to know where/how to use it
maybe even adda tag to my object...
Does '$$anonymous$$ain Frame' have a collider? If not, a raycast wont hit it. Tags have nothing to do with it (layers do, I doubt that's your issue though).
In what way is it not working?
Answer by Khada · Feb 23, 2013 at 05:32 PM
To hit an object with a raycast, it needs a collider. To add a collider to an object:
Select object
Click 'Add Component' in the inspector
Click on 'Physics'
Select any of the colliders (mesh, box, sphere, capsule, etc).
In your code you will need to check if you have hit the right object. This can be done by comparing names (and many other ways). Eg:
using UnityEngine;
using System.Collections;
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.name == "Main Frame")
Application.LoadLevel("Barracks");
}
}
}
}
The script needs to be attached to a game object in the scene in order for Update to be called.
sorry, I dont know what's not working, the object has the Sphere Collider (since its a sphere) and I inserted your changes, but when I click it still doenst go to the Barracks
Then your issue is very likely somewhere else, because the code I've posted should work fine. I can't tell you what's wrong without knowing the rest of your code so can you post the entire script up for me to look through?
then probably that's the entire problem, since that's my whole script...
Answer by MountDoomTeam · Feb 23, 2013 at 05:22 PM
search the net for hit.gameObject for related questions
Try this:
if(hit.gameObject.tag == "EnemyBullit") { PlayerStats.HEALTH -= 20;}
And also hit.gameObject.name ==
print(hit.gameObject.name)
http://answers.unity3d.com/questions/21338/a-better-way-then-ifhitgameobjecttag.html
Answer by Paparakas · Mar 06, 2013 at 04:54 AM
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.