Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gigica04 · Feb 23, 2013 at 04:50 PM · c#gameobjectraycastbeginnerhit

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!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Khada · Feb 23, 2013 at 05:03 PM 0
Share

Looking at your code and reading your question, I have no idea what you're asking. Can you try giving more detail?

avatar image gigica04 · Feb 23, 2013 at 05:10 PM 0
Share

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...

avatar image Khada · Feb 23, 2013 at 05:18 PM 0
Share

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?

avatar image gigica04 · Feb 23, 2013 at 05:20 PM 0
Share

no it doesnt, how do I make it have one?

avatar image Khada · Feb 23, 2013 at 05:33 PM 0
Share

Posted an answer for you.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 21 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gigica04 · Feb 23, 2013 at 05:50 PM 0
Share

not working still :/

avatar image Khada · Feb 23, 2013 at 06:05 PM 0
Share

What part isn't working? I'm not telepathic.

avatar image gigica04 · Feb 23, 2013 at 08:18 PM 0
Share

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

avatar image Khada · Feb 24, 2013 at 12:39 AM 0
Share

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?

avatar image gigica04 · Feb 24, 2013 at 02:12 AM 0
Share

then probably that's the entire problem, since that's my whole script...

Show more comments
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect if a raycast ray stop hitting an object 1 Answer

why is part of my script being ignored? 2 Answers

AI Instantiate targets/prefab. (pick target issue) 1 Answer

Getting game logic object from GameObject 1 Answer

Detect Object that are hit by a ray 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges