Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 adambarakat3 · May 17, 2021 at 07:13 PM · raycastunity 2draycasthit2d

Raycast2D not returning any transform instead keeps returning void.

 public class Raycasting : MonoBehaviour
 {
     Vector2 mousePosition;
     // Start is called before the first frame update
     void Start()
     {
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         if (Input.GetMouseButtonDown(0))
         {
             mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
             Debug.Log("Raycast");
             RaycastHit2D hit = Physics2D.Raycast(mousePosition,  transform.forward, 100.0f);
             Debug.DrawRay(mousePosition,  this.gameObject.transform.TransformDirection(Vector3.forward), Color.green);
             Debug.Log(hit);
             Debug.Log(hit.transform);
             if (hit.transform != null)
             {
                 Debug.Log(hit.transform.gameObject);
             }
         }
     }
 }

I'm not getting any errors, but the hit.transform keeps giving null. the targeted gameObject has a collider and evrything.

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 Zelacks · May 18, 2021 at 09:01 AM

You need to give more information about what you are trying to achieve. When hit.transform is null, it means you are not hitting anything. I am going to assume you are trying to make it so you get the transform of whatever you click on (I could be completely wrong but I am just guessing due to a lack of information).

The main problem is that you need to transform the mouse click to a world position, since Input.mousePosition is not a world position but rather a position on the screen.

 var clickPosition = (Vector2)(Camera.main.ScreenToWorldPoint( Input.mousePosition ));


This should change it so you get a world position, based on the position of your main camera and where the user clicked.

Also modifying the draw ray, to have a duration (without the duration the ray immediately disappears and wont be visible) and a direction to the right (it doesnt represent the ray we are making but its close enough to atleast debug where the ray is being directed).

             Debug.DrawRay(clickPosition,  Vector2.right, Color.green, 5f);


To finish off, we just need to use our click Position, and a direction of zero (meaning directly on the point) to get what is under the cursor

          RaycastHit2D hit = Physics2D.Raycast(clickPosition, Vector2.zero);

Full code:

 using UnityEngine;
 
 
 public class Raycasting : MonoBehaviour
 {
     Vector2 mousePosition;
     // Start is called before the first frame update
     void Start()
     {
     }
  
     // Update is called once per frame
     void FixedUpdate()
     {
         mousePosition = Input.mousePosition;
         if (Input.GetMouseButtonDown(0))
         {
             var clickPosition = (Vector2)(Camera.main.ScreenToWorldPoint( Input.mousePosition ));
             
             RaycastHit2D hit = Physics2D.Raycast(clickPosition, Vector2.zero);
             
             Debug.DrawRay(clickPosition,  Vector2.right, Color.green, 5f);
             
             Debug.Log(hit);
             Debug.Log(hit.transform);
             if (hit.transform != null)
             {
                 Debug.Log(hit.transform.gameObject);
             }
         }
     }
 }
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

173 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

Related Questions

How to get co-ordinates of end point of Raycast2D ? 1 Answer

Raycasthit2d isnt working. 2 Answers

Raycast2D/Line Renderer offset on colliders based on mouse position 0 Answers

2D Raycast effect only what it hits? 2 Answers

2D - finding object at mouse position 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