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 laurikens · Mar 22, 2014 at 04:57 PM · c#raycastray

Rays not hitting (most of the time)

So i'm making a first person shooter and im using this code to shoot a ray:

 ray = GameObject.FindGameObjectWithTag("MainCamera").camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0))
 
 if (Physics.Raycast(ray, out hit, 1000.0f))


Sometimes this works, but most of the time the hit never registers with the enemy.
here is a picture showing where the rays are being shot and also showing the box collider for the enemy.
http://i.imgur.com/aJJKvGz.png When I print to the console what I have hit, its just saying that I hit the terrain, or something else that is behind the enemy.

Comment
Add comment · Show 12
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 getyour411 · Mar 22, 2014 at 04:58 PM 0
Share

Based on your screenshot and console output (lack of), sounds like your 'box collider' is not getting hit maybe because of its tag, physics layer, etc, so the ray is shooting through

avatar image laurikens · Mar 22, 2014 at 05:09 PM 0
Share

I'm using Debug.Drawray to see where its going, and its going straight through the enemies box collider.
I spawn 10 enemies from the same prefab and some of them NEVER can get hit.
I stop the program and start again and the ones I couldn't hit before are now detecting hits and others wont be able to get hit.
I really have no idea whats wrong, im pretty sure its something wrong with unity...

avatar image getyour411 · Mar 22, 2014 at 05:12 PM 0
Share

What layer is your box collider on? Look at Edit / Project Settings / Physics - does that layer interact with the layer shooting the Ray? Show the code for your actual raycast "hit" logic?

avatar image supernat · Mar 22, 2014 at 05:18 PM 0
Share

Is there a pattern? Such as it always hits the first one created, or it only hits instances that were in the scene already, etc? Can you please post more of your raycast code above, specifically after the if statement?

avatar image laurikens · Mar 22, 2014 at 05:18 PM 0
Share

Here are the settings you're talking about.
http://i.imgur.com/Lfl$$anonymous$$oDP.png
Everything is on the default layer.
I found that moving to a different location can sometimes stop the ray from passing through the enemy without triggering a hit, still no idea what is causing this though, because I can kill other enemies at that location and others I cant.

Here is all the code for my raycasting

 ray = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera").camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
             if (Physics.Raycast(ray, out hit))
             {
                 Debug.Log("Hit: " + hit.transform.name + ".\n");
                 if(hit.transform.tag=="Enemy")
                 {
                     hit.transform.Send$$anonymous$$essage("Hit");
                 }
                 Debug.DrawLine(transform.position, hit.point, Color.red, 200);
             }
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Raycasting ignoring small sized object 1 Answer

Getting the Furthest Point on a Ray 1 Answer

AI Pathfinding using RayCast 0 Answers

For each Raycast? 1 Answer

Setting a point with a ray C# 1 Answer


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