Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
-1
Question by Parzivell · Jul 04, 2015 at 03:39 AM · raycastcolliderraycasthit

Using raycast and collider to increase int

I looked at the raycast documentation script on the unity website to try and understand raycasts. I was using the script and was wondering how i can change it to make the raycast know if it hit a certain collider then increase an int by 1 if it hit the correct collider. (the code sample button isnt working properly so i have to insert the code here)

//

//

using UnityEngine; using System.Collections;

public class Raycast : MonoBehaviour {

 public float RayDistance = 5;

 void Update() {

     if (Input.GetButtonDown ("Fire1")) 
     {
         Vector3 fwd = transform.TransformDirection (Vector3.forward);
         if (Physics.Raycast (transform.position, fwd, RayDistance))
             print ("There is something in front of the object!");
     }     
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Tomer-Barkan · Jul 04, 2015 at 05:28 AM

You need to assign the result of the raycast into a RaycastHit, there you have a reference to the collider that was hit, and from it you can get any component of that object and check it's tag, name, or anything else you want.

 RaycastHit hitInfo;
 if (Physics.Raycast (transform.position, fwd, out hitInfo, RayDistance)) {
    GameObject hitObject = hitInfo.collider.gameObject;
    if (hitObject.tag == "sometag") {
        // Do something
    }
 }


Comment
Add comment · Show 2 · 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 Parzivell · Jul 05, 2015 at 02:35 AM 0
Share

I tried that but its not increasing the int

 using UnityEngine;
 using System.Collections;
 
 public class ResourcesGather : $$anonymous$$onoBehaviour {
 
     public float RayDistance = 5;
     public RaycastHit hitInfo;
     //RESOURCE TYPES
     public int Wood = 0;
 
     void Update() {
 
         if (Input.GetButtonDown ("Fire1")) 
         {
             Vector3 fwd = transform.TransformDirection (Vector3.forward);
             if (Physics.Raycast (transform.position, fwd, out hitInfo, RayDistance)) 
             {
                 print ("There is something in front of the object!");
                 GameObject hitObject = hitInfo.collider.gameObject;
                 if (hitObject.tag == "Tree") 
                 {
                     print("You hit a tree, have some wood");
                     Wood += 1;
                 }
             }     
         }
     }
 }


I'm only seeing the message "There is something in front of the object!" I'm not seeing the message "you hit a tree, have some wood" and the int isn't increasing either. Any ideas?

avatar image AlucardJay · Jul 05, 2015 at 02:49 AM 1
Share

Debug to identify what you hit :

 print ("There is something in front of the object! name = " + hitInfo.collider.gameObject.name + ". tag = " + hitInfo.collider.gameObject.tag );

Please note if you are trying to raycast a tree that is painted on the terrain, it will not show as tree but terrain. Painted tree colliders become part of the terrain collider.

http://answers.unity3d.com/questions/685405/mass-place-trees-tagging.html

http://answers.unity3d.com/questions/650308/how-do-i-interact-with-terrain-trees.html

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RaycastHit.transform/collider 1 Answer

Difference between hit.collider.gameObject vs hit.transform.gameObject 2 Answers

Get collider from raycast 4 Answers

RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag and which should I use when? 1 Answer

Is there a way to detect if an object is being hit by a raycast? 2 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