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 /
avatar image
0
Question by Nebur9 · Feb 04, 2020 at 08:46 PM · collisionraycastcollision detectionraycasthitissue

Raycast not Detecting Colliders

Raycast does not seem to be hitting colliders. My walls are in their own layer but putting them in the default layer did not change anything. The Debug.DrawRay() and Debug.Log(hi1.transform.name) are not returning anything but I'm sure the Shoot() method is being invoked because of my other Debug.Logs.

public class ShootP1 : MonoBehaviour { public GameObject Shooter;

 // Update is called once per frame
 void Update ()
 {
     if (Input.GetKeyDown("r"))
     {
         Shoot();
         //Debug.Log("Pressed");
     }
 }

 void Shoot()
 {
        RaycastHit hit1;
        //Debug.Log("shot");
        
        if (Physics.Raycast(Shooter.transform.position, Shooter.transform.forward, out hit1, 1000))
        {
            Debug.DrawRay(Shooter.transform.position, Shooter.transform.forward, Color.cyan);
            Debug.Log(hit1.transform.name);
        }
 }
 
Comment
Add comment · Show 1
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 highpockets · Feb 04, 2020 at 10:01 PM 0
Share

You might want to try that DrawRay() before the Raycast to see where your ray is actually being shot. I’d suggest you start there

EDIT: Put the DrawRay right in the update so you can see where it’s shooting off to at all times. Quite sure it’s missing your target

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kylecat6330 · Feb 05, 2020 at 06:41 AM

It might be an issue with the Shooter.transform.forward being a local direction in which case just add a transform.TransformDirection into the ray cast.

 Physics.Raycast(Shooter.transform.position, Shooter.transform.TransformDirection(Vector3.forward), out hit1, 1000)

That would be my best guess, but I am no expert. If that doesn't work you should put a Debug.DrawRay before the ray cast to make sure it is actually going where its is supposed to.

  Debug.DrawRay(Shooter.transform.position, Shooter.transform.forward * 1000, Color.cyan);
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 kskjadav007 · Feb 05, 2020 at 09:48 AM

     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown("r"))
         {
             Shoot();
             Debug.Log("Pressed");
         }
     }
     void Shoot()
     {
 
         RaycastHit hit;
         // Does the ray intersect any objects excluding the player layer
         if (Physics.Raycast(Shooter.transform.position, Shooter.transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
         {
             Debug.DrawRay(Shooter.transform.position, Shooter.transform.TransformDirection(Vector3.forward) * hit.distance, Color.green,10f);
             Debug.Log("Did Hit");
 
             Debug.Log(hit.transform.name);
 
         }
         else
         {
             Debug.DrawRay(Shooter.transform.position, Shooter.transform.TransformDirection(Vector3.forward) * 1000, Color.red,2f);
             Debug.Log("Did not Hit");
         }
 
     }

this is working

Checkout this link

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

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

Raycasting to detect which side of collider is hit 2 Answers

Raycast exit point of collider 0 Answers

RayCast2D problems on collision detection 3 Answers

Physics raycast hit offset from where it should be 1 Answer

Fall collision force with help of raycast 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