• 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 TristenMengis · Jul 16, 2017 at 12:54 AM · collidersraycastinglayersraycasthit2d

Raycast2D doesn't seem to work correctly.

Hello,

I'm trying to make a simple top-down game with guns, but the script used to shoot the gun does not seem to be working. The following code block is part of a script that is attached to the player. This method is called every time the player presses the mouse:

 void PistolShot()
     {
         Vector2 origin = transform.position;
         Vector2 end = mainCamera.ScreenToWorldPoint(Input.mousePosition);
         Vector2 direction = (end - origin).normalized;
         int Layer = LayerMask.GetMask("Shootable");
         RaycastHit2D hit = Physics2D.Raycast(origin, direction);
         if( hit.collider.IsTouchingLayers(Layer))
         {
             GameObject targetObject = hit.transform.gameObject;
             Enemy target = targetObject.GetComponent<Enemy>();
             if (target != null)
             {
                 target.TakeDamage(10);
             }
         }
     }

Also, the enemy script looks like this:

 public class Enemy : MonoBehaviour {
 
     public int health;
 
     void Start()
     {
         health = 50;
     }
 
     public void TakeDamage(int damage)
     {
         health -= damage;
         if(health <= 0)
         {
             Destroy(this.gameObject);
         }
     }
 }

The problem is that when I click to use the gun, either the raycast doesn't hit the enemy's collider, or it doesn't seem to be calling the TakeDamage method on the enemy. Either way, when I try to shoot at the enemy, it doesn't make the health any lower. When I use Debug.DrawLine(origin, end), the line drawn seems to go right through the enemy, but I don't think the ray is being cast correctly.

Any help would be greatly appreciated!

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 TristenMengis · Aug 03, 2017 at 08:44 PM 0
Share

I found that when I position the character directly above the "enemy," it will deal the damage, however it only happens when I'm directly above. Why doesn't the ray seem to be going infinitely far until it hits a collider? It's like it only goes a small distance away from the player character.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Walter_Hulsebos · Jul 16, 2017 at 06:51 AM

Alright, it doesn't work correctly? What does it refuse to do?

Comment
Add comment · Show 2 · 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 TristenMengis · Jul 16, 2017 at 04:42 PM 0
Share

Haha! I felt like I forgot something! The problem is that when I click to use the gun, either the raycast doesn't hit the enemy's collider, or it doesn't seem to be calling the TakeDamage method on the enemy. Either way, when I try to shoot at the enemy, it doesn't make the health any lower. When I use Debug.DrawLine(origin, end), the line drawn seems to go right through the enemy, but I don't think the ray is being cast correctly.

avatar image TristenMengis · Aug 03, 2017 at 08:41 PM 0
Share

Has this question already been asked? I'm stumped. I wish I could show you what it does, but I don't know how.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

72 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

Related Questions

Right Click to Ignore All But Terrain, How? 1 Answer

Best method to make the main character traverse multiple layers of objects using Unity 2D 1 Answer

What's wrong with my RayCastHit2D. Raycast only on positive values on horizontal axis and on negative values on vertical axis 0 Answers

How do I make raycast ignore the players collider 1 Answer

How do I create a wide beam thats visible to the player and is stopped by barriers in my 2D stealth game? 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