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
2
Question by Chris 35 · Apr 22, 2011 at 01:46 PM · javascriptgameobjectraycastraycasthithit

Detect Object that are hit by a ray

I have this script which casts a ray when the mouse button is clicked

var Bullet1_direction = Vector3(15,0,45); var Bullet2_direction = Vector3(0 ,0,1); var Bullet3_direction = Vector3(-9 ,0,45); var length = 4;

function Update () {

var Bullet1_diagonal = transform.TransformDirection(Bullet1_direction); Bullet1_diagonal.Normalize();

var Bullet2_diagonal = transform.TransformDirection(Bullet2_direction); Bullet2_diagonal.Normalize();

var Bullet3_diagonal = transform.TransformDirection(Bullet3_direction); Bullet3_diagonal.Normalize();

if(Input.GetMouseButtonDown(0)){

 Debug.DrawRay(transform.position, Bullet1_diagonal * length,Color.red);
 Debug.DrawRay(transform.position, Bullet2_diagonal * length,Color.red);
 Debug.DrawRay(transform.position, Bullet3_diagonal * length,Color.red);

 if (Physics.Raycast(transform.position, Bullet1_diagonal,length)) {
   Debug.Log ("Bullet 1 hit red!");
 }
 if (Physics.Raycast(transform.position, Bullet2_diagonal,length)) {
   Debug.Log ("Bullet 2 hit red!");
 }
 if (Physics.Raycast(transform.position, Bullet3_diagonal,length)) {
   Debug.Log ("Bullet 3 hit red!");
 }

}

}

what i want to do it make it so what ever object the ray hits it returns the name of the object and destroys that object. for example im using it for a top down ish shooter game so the object it hits will be an enemy however there will be more then one of the same enemy in the scene at once so can you make it that what ever object the ray hits to destroy that singular ibect not every one of thoes object?

help would be awesome

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bryan 4 · Apr 22, 2011 at 03:29 PM

you can use an out RaycastHit as one of the parameters for Physics.Raycast (look at its overloaded options) Then

if(Physics.Raycast())
{
 hit.collider.gameObject <---- the game object that was hit.
}
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 Chris 35 · Apr 23, 2011 at 01:59 AM 0
Share

should'nt hit.collider.gameObject be part of the if statement? how would i use it in relation to my code ?

avatar image Chris 35 · Apr 23, 2011 at 02:11 AM 0
Share

ive posted the code you gave me below and how im trying to use it...

avatar image
0

Answer by Chris 35 · Apr 23, 2011 at 02:10 AM

@ Bryan this is how im interpretation the code you gave me

if (Physics.Raycast(transform.position, Bullet1_diagonal,length)) { if(Physics.Raycast(hit.collider.gameObject)){ if(gameObject.tag == ("Enemy")){ Debug.Log ("kill");

} } }

except i get a null reference error Help?

Comment
Add comment · Show 1 · 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 MC HALO · Apr 23, 2011 at 04:21 AM 0
Share

that will give you errors because you can not do :

 if(Physics.Raycast(hit.collider.gameObject)){

it should be like :

 if(hit.collider.gameObject.tag == "Enemy1"){
 Destroy(hit.collider.gameObject);

have a look at my answer below :)

avatar image
0

Answer by MC HALO · Apr 23, 2011 at 04:16 AM

Well from what i read i hope is correct you have multiple enemies right and when the ray collides with the bad guy you want to destroy the game object. Now the easiest way that i can think of is creating variables at the top which carry gameObject in this case your enemies. every time your ray collides variable set gameObject with the enemy in it the ray will destroy the gameObject with the right tag. here is what i would do:

var Bullet1_direction = Vector3(15,0,45);

 var Bullet2_direction = Vector3(0 ,0,1);

  var Bullet3_direction = Vector3(-9 ,0,45);

   var length = 4;



     var Enemy1 : GameObject;

       var Enemy2 : GameObject;

       var Enemy3 : GameObject;





    function Update () {



           var Hit : RaycastHit;



    var Bullet1_diagonal = transform.TransformDirection(Bullet1_direction);

    Bullet1_diagonal.Normalize();



   var Bullet2_diagonal = transform.TransformDirection(Bullet2_direction);

    Bullet2_diagonal.Normalize();



   var Bullet3_diagonal = transform.TransformDirection(Bullet3_direction);

     Bullet3_diagonal.Normalize();





    if(Input.GetMouseButtonDown(0)){





    Debug.DrawRay(transform.position, Bullet1_diagonal * length,Color.red);

         Debug.DrawRay(transform.position, Bullet2_diagonal * length,Color.red);

             Debug.DrawRay(transform.position, Bullet3_diagonal * length,Color.red);



  if (Physics.Raycast(transform.position, Bullet1_diagonal,Hit,length)) {

       Debug.Log ("Bullet 1 hit red!");

         if(Hit.collider.gameObject.tag == "Enemy1"){

             Destroy(Hit.collider.gameOject); 



  }



 }



if (Physics.Raycast(transform.position, Bullet2_diagonal,Hit,length)) {

   Debug.Log ("Bullet 2 hit red!");

      if(Hit.collider.gameObject.tag == "Enemy2"){

         Destroy(Hit.collider.gameOject); 



   }



      }

 if (Physics.Raycast(transform.position, Bullet3_diagonal,Hit,length)) {

   Debug.Log ("Bullet 3 hit red!");

      if(Hit.collider.gameObject.tag == "Enemy3"){

       Destroy(Hit.collider.gameOject); 



       }

    }

 }



}

Now the way that this works is really simple if the ray hits the following enemy stored in the gameObject it will destroy it. don't forget to assign the game object in your inspector mode. If this does not work please let me know thanks :)


Problem Update :


i have solved your problem :) basically the problem was in the ray itself, this is because if you remember on the top of update i created a variable called var Hit: RayCastHit

this became a major part in your ray because every time it would shoot the ray out i was told to destroy an object that was stored in the hit variable. So all i need to do was add the following to the following line:

if (Physics.Raycast(transform.position, Bullet1_diagonal,Hit,length)) {

   Debug.Log ("Bullet 1 hit red!");

     if(Hit.collider.gameObject.tag == "Enemy1"){

         Destroy(Hit.collider.gameObject); 

i have pasted the code that had the changes you will now notice that after diagonal i have placed "HIT" Now we have told the ray once you shoot out your diagonal bullet look for the stored object in the hit. And then at the bottom we have just stored the hit to enemy 1 and then destroy the object. now the second thing you may find different is the Destroy call instead of using the 3 var gameobjects at the top i just told it to destroy the object that is collided with the hit in this case an Enemy that has the tag "Enemy1"

hope it helps :)

Comment
Add comment · Show 5 · 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 Chris 35 · Apr 23, 2011 at 01:14 PM 0
Share

I keep getting a null reference error Object reference not set to an instance of an object and the error keep pointing me to theses three lines of code if(Hit.collider.gameObject.tag == "Enemy1"){ if(Hit.collider.gameObject.tag == "Enemy2"){ if(Hit.collider.gameObject.tag == "Enemy3"){

avatar image MC HALO · Apr 23, 2011 at 04:26 PM 0
Share

have your enemies been tagged with the names Enemy1 , 2,3 and then tried if it still did not work please send me your file on my google account and i will have a look for you

my e-mail address

Hummad.Nazir@gmail.com

or my hotmail

gto_oni-eyes@hotmail.com

avatar image MC HALO · Apr 23, 2011 at 04:26 PM 0
Share

hope to hear from you

avatar image Chris 35 · Apr 24, 2011 at 06:10 AM 0
Share

i sent you an email on your gmail email containing the project with the script

avatar image MC HALO · Apr 24, 2011 at 04:06 PM 0
Share

i have fixed it :) i am going to update the answer if other people need help :)

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

1 Person is following this question.

avatar image

Related Questions

Need help with building system 1 Answer

Creating a teleportation gun 1 Answer

How to detect raycast exiting 1 Answer

Ray Casting - Trigger function 1 Answer

Raycast and RaycastHit failure 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