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 drpelz · Apr 09, 2021 at 09:22 PM · 2d gamephysics2draycasthit2d

How to stop raycast when hitting an object in Unity?

Hi,

in my Unity game (top-down 2D shooter) there are some enemies flying along a path (via DOTween). My player casts a ray into the scene in order to get an enemy-object right in front of the player. So far so good.

Now the problem is that I want only one enemy-object as a result, i.e. the raycast should stop when it hits an enemy-object for the very first time. How do I achieve this?

I need only one enemy-object hit by the raycast because there is a crosshair in my game and when the raycast starts and the enemies fly along the path the crosshair jumps forth and back (and I don't want this - the crosshair should stay at the first enemy hit by the raycast). Any help, please?

raycast-video: https://imgur.com/a/KBngql8

This is my code (attached to the player):

 void FixedUpdate() {
     
     //crosshair: Cast a ray straight up.
     float _size = 12f;
         Vector2 _direction = this.transform.up;
         RaycastHit2D _hit = Physics2D.Raycast(this.transform.position, _direction, _size);

       if (_hit.collider != null && _hit.collider.tag == "EnemyShipTag") {

         // We touched something!

         Vector2 target = new Vector2(_hit.collider.gameObject.transform.position.x, _hit.collider.gameObject.transform.position.y);

         const float moveTime = 0.1f;
         float step;

         step = Vector2.Distance(crosshairGO.transform.position, target);
         crosshairGO.transform.position = Vector2.MoveTowards(crosshairGO.transform.position, target, step / moveTime * Time.deltaTime);

         Vector2 _pos3 = new Vector2(this.transform.position.x, crosshairGO.transform.position.y);
         crosshairGO.transform.position = _pos3;
         crosshairBegin = false;
     } else {
         // Nothing hit

         Vector2 _pos2 = new Vector2(this.transform.position.x, 4.5f);
         if (crosshairBegin) {
             crosshairGO.transform.position = _pos2;
         } else {
             Vector2 _pos4 = new Vector2(this.transform.position.x, crosshairGO.transform.position.y);
             crosshairGO.transform.position = _pos4;
         }
     }


 }
Comment
Add comment · Show 2
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 ryanlin138 · Apr 10, 2021 at 12:07 AM 1
Share

Your chain of enemies has some gaps in it, and the raycast is going through the gaps in the colliders. To stop the jumping, try making the colliders larger or the enemies closer together.

If you want the raycast to stay at the first enemy, then you have to store the transform of the first enemy you hit. Then check if the enemy still exists and move the crosshair to the position of the stored transform. If the enemy doesn't exist anymore, then use raycast to find more enemies.

avatar image drpelz · Apr 10, 2021 at 12:12 AM 0
Share

Wow! Very interesting because I didn't realize that the gaps could be the problem. So I think I have to make them bigger - if I place the enemies closer together it might look silly. I will check this out ASAP! Thanks for this great hint, man!:)

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

153 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

Related Questions

Raycast 2D hits sprite with collider, but returns error when not hitting any collider 2 Answers

How do I do a Snappy Jump in 2D with a 2D Jump Script? 0 Answers

Get the name of the collided object 2 Answers

RaycastHit2D should be child object but is parent 1 Answer

Hey...I wanted to make a 2d mining game. And my question is: *How can i check if the player keeps constantly holding the left mouse button on a block for like 2 seconds to mine it(via Raycast)?*...Thank u <3 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