Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 TheAlmightyPixel · Jul 10, 2015 at 07:29 PM · 2dphysicsraycastmouse

(C#) 2D Raycast wrong direction

Hi all, So I've been trying to dig into 2D for the first time, and I've immediately gotten stuck with raycasting.

What I'm trying to do is have a ray be cast from the barrel of a gun to the position of the mouse in 2D space. This is the code I have now:

 if(Input.GetButton("Fire1"))
         {
             if(canShoot)
             {
                 timer = rateOfFire;
                 bulletsLeft -= 1;
 
                 canShoot = false;
 
                 RaycastHit2D hit = Physics2D.Raycast(gunBarrel.position, (Camera.main.ScreenToWorldPoint(Input.mousePosition) - gunBarrel.position), range);
 
                 if(hit.collider != null)
                 {
                     if(hit.collider.tag == "EnemyBody" || hit.collider.tag == "EnemyHead" || hit.collider.tag == "Enemy")
                     {
                         enemy_health = hit.collider.gameObject.GetComponentInParent<EnemyHealth>();
                     }
                     
                     if(hit.collider.tag == "EnemyBody")
                     {
                         enemy_health.ReceiveDamage(damage, 2);
                     }
                     else
                         if(hit.collider.tag == "EnemyHead")
                     {
                         enemy_health.ReceiveDamage(damage, 1);
                     }
                     else
                         if(hit.collider.tag == "Enemy")
                     {
                         enemy_health.ReceiveDamage(damage, 3);
                     }
                 }
             }
         }

What's happening now is that the ray is cast into a completely wrong direction, and nowhere near where the mouse is. None of the objects here are children of another object.

I know this issue has been adressed a few times here already, but I haven't found my answer here yet. Thanks in advance.

EDIT I noticed that Unity is able to get my mouse's position in pixel coordinates, but when it turns those coordinates into world space coordinates, it just gives them the same value as my main camera's coordinates. Does anyone know why this would be happening?

Comment
Add comment · Show 4
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 _Gkxd · Jul 11, 2015 at 06:22 AM 0
Share

Use Debug.Log to see the numbers that you're actually giving to the raycast, and use Debug.DrawRay or Debug.DrawLine to see the ray in the scene view. That should help you narrow down your problem.

avatar image TheAlmightyPixel · Jul 11, 2015 at 09:59 AM 0
Share

I've already tried that, but it doesn't really help with the problem. Updated the question.

avatar image zohaib817 · Jul 11, 2015 at 10:47 AM 0
Share

RaycastHit2D hit = Physics2D.Raycast(gunBarrel.position, (Camera.main.ScreenToWorldPoint(Input.mousePosition) + gunBarrel.position), range);

re_edit this line like this or check the rotation of gunBarrel :)

avatar image TheAlmightyPixel · Jul 12, 2015 at 03:18 PM 0
Share

There's nothing wrong with the rotation of the gunBarrel object, and that didn't seem to work. I may have to look into some other way of doing the raycasting here.

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

23 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

Related Questions

Why does Physics2D.Capsule/Circle/BoxCast's hit point and distance not line up? 0 Answers

Raycast2D always "remembers" it's last hit 2 Answers

Linecast, rotate end point relative to objects rotation 1 Answer

HOW DO I USE RAYCASTHIT 2D???? 1 Answer

2D Slope acceleration 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