Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by kanaron · Mar 20, 2016 at 09:10 PM · raycast

How to get first object in which Ray hit?

Hi, I have problem with get first object in which ray hit. I have two same objects in line. When i cast ray it come through first and second object and then return rayHit in the second collider. How can i get the first collider?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Vylax · Mar 20, 2016 at 09:22 PM

You can take the first collider by some different solution : 1. When the raycasthit hit something, make the script executing the commands and destroy after (it would be shorter to don't hit the second one). 2. use something like this :

 var aGameObject : GameObject;
 var aNumber : int = 1;
 ...
 
 if("I don't remember the code for "if the raycasthit hit something" and I'm on phone so do this yourself" && aNumber == 1) {
 aGameObject = "the object you hit (I don't remember the code to)";
 aNumber = 2;
 }

and here's other way to do it but I forget these while writing ^^ good luck with my bad envlish ^^ I hope it will help you

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 kanaron · Mar 21, 2016 at 07:12 AM

Thanks, but that don't work. I have script:

 ...
 Debug.DrawRay(pos4, (pos2 - pos4), Color.red, Vector3.Distance(pos4, pos2)); //for see raycast
         if (Physics.Raycast(pos4, (pos2 - pos4), out rayHit, Vector3.Distance(pos4, pos2))) //pos2 = actual position, pos4= position 2 frames ago
         {
         ...
         Debug.Log("HIT in " + rayHit.collider.name); // for see in what objects ray hit.
         ...
         }
 

And this script works good. But Raycast don't work. I see the raycast is one frame too slow, but that i think doesn't matter. The main problem is with hit detection:

First i see raycast hit in object in which i cast ray. That's correct.

Then ray comes through plane, and this hit was detect. That's correctt too.

But now: I have object with left and right collider (left and right armor): First is right armor. Ray comes through and... don't detect collision... I should see in console "HIT in Right armor" but there is nothing. And then is left armor. And that collider is detected and script make some damage. But that damage should be in right side, not left.

Comment
Add comment · Show 4 · 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 Vylax · Mar 21, 2016 at 11:37 AM 0
Share

can you add screenshots or a video for help mento visualize please ?

avatar image kanaron · Mar 21, 2016 at 12:00 PM 0
Share

Okay, i find where is the problem: i have quite much colliders there, about which i forgot and that colliders are before (very near) armor colliders. So the line of ray was: 1. cast ray 2. plane 3. other collider and very near armor collider which wasn't detected.

So i change positions of armor colliders to position just before that other colliders and now everything works perfectly :D

avatar image Vylax · Mar 21, 2016 at 12:35 PM 0
Share

can you don't use raycast an use an object (like a bullet) wich had a box collider and jse the function OnCollisionEnter like this :

 function OnCollisionEnter (other : Collision) {
 if(canhit == true)
 if(other.gameObject.tag =="left side" || other.gameObject.tag == "right side") {
 var thehitobject : GameObject = other.gameObject;
 DoWhatYouWant();
 canhit = false;
 }
 }
 }
 
 function DoWhatYouWant () {
 print("do something");
 }

tell me what you get

avatar image kanaron · Mar 21, 2016 at 01:31 PM 0
Share

In fact i don't use raycast only. I use bullet with rigidbody because i need artillery shot so i need curve line of fall, not just straight line like raycast. I try to use only object with collider without raycast, but that doesn't work. Raycast is just between position of bullet from two frames ago and current position. And i use raycast for detecting hit and check angle of hit.

And i can't use only collider and OnCollisionEnter(). I must use object with sphere collider and raycast because my bullet have very high start speed (it is very important). In some cases the speed is so high and armor (object) is so small that very often bullet just comes through all object without detecting collision because in one frame bullet was just before armor and in next frame bullet was behind object or inside object. In that case there was no detect of hit or hit wasn't detect correctly (angle of collision was detected like 90* when in fact that was near 0*).

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

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

how to check if an object is betwen enemy and player 1 Answer

Why am I getting a Null Reference Exception? 0 Answers

raycast causing wierd bouncing 2d 0 Answers

I can't store raycast hit data in a variable?! 0 Answers

Enemy is loosing life when he's gun is shot 0 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