Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 ParyPro · Dec 24, 2019 at 01:48 PM · raycastraycastingraycasthit2d

Is there a problem with my raycast?

Hello! I've been tearing my hair out over this problem for a few days now.. & at this point I'm void of any more solutions to try.

It's a very simple game, my little square bounces across platforms to the finish line to win. What I wanted to do is have him be able to shoot a ray that destroys the enemies (spheres) upon any impact with the ray. This ray would be controlled by holding down the mouse in any direction to shoot from the Player position to the target (mouse click pos). I'd already been successful making my enemies a ray to rotate around for my player to dodge, so I figured I could just copy the same logic and tweak it.

alt text alt text

My quest eventually led me to create two different scripts, each now currently doing 1/ 2 of what I need. The images attached show one script in action and then the other. My playerlazerray1 script allows me to move the ray in any direction the mouse is at when it's clicked and held, however, upon touching enemies my ray does not trigger their destruction unless I move the ray very close. With my playerlazerray2 script, enemies are destroyed from any distance upon colliding but the line of sight hardly moves with my raycast (as seen in image attached). PlayerLazerRay1 script:

  public class PlayerLazerRay : MonoBehaviour {
  
      public float distanceRayCanSee;
  
      public LineRenderer playerLineOfSight;
      public Gradient redColor;
  
      private Shake shake;
      private Vector2 target;
  
      bool PlayerShootingLazer;
  
      void Start()
      {
          shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>();
          
  
          Physics2D.queriesStartInColliders = false;
      }
  
  
      void Update()
      {
          
  
          if (Input.GetMouseButton(0))
          {
              playerLineOfSight.enabled = true;
              PlayerShootingLazer = true;
              Invoke("EndLazer", 2.5f);
          }
  
          if (PlayerShootingLazer == true)
          {
              target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
              RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, target, distanceRayCanSee + 50);
  
              if (hitInfo.collider != null)
              {
                  Debug.DrawLine(transform.position, target, Color.red);
                  playerLineOfSight.SetPosition(1, target);
                  playerLineOfSight.colorGradient = redColor;
  
                  if (hitInfo.collider.CompareTag("Enemy"))
                  {
                      print("YES");
                      Destroy(hitInfo.collider.gameObject);
                      hitInfo.collider.gameObject.SetActive(false);
                  }
              }
              else
              {
                  Debug.DrawLine(transform.position, target * distanceRayCanSee, Color.red);
                  playerLineOfSight.SetPosition(1, target * distanceRayCanSee);
                  playerLineOfSight.colorGradient = redColor;
              }
  
              playerLineOfSight.SetPosition(0, transform.position);
          }
      }
  
      void OnTriggerEnter2D(Collider2D other)
      {
          shake.CamShake();
  
      }
  
      void EndLazer()
      {
          playerLineOfSight.enabled = false;
          PlayerShootingLazer = false;
      }
  }


PlayerLazerRay2 script:

 public class PlayerLAzerRay2 : MonoBehaviour {
  
      public float distanceRayCanSee;
  
      public LineRenderer playerLineOfSight;
      public Gradient redColor;
  
      private Shake shake;
      private Vector2 target;
  
      bool PlayerShootingLazer;
  
      void Start()
      {
          shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>();
          
  
          Physics2D.queriesStartInColliders = false;
      }
  
  
      void Update()
      {
          
  
          if (Input.GetMouseButton(0))
          {
              playerLineOfSight.enabled = true;
              PlayerShootingLazer = true;
              Invoke("EndLazer", 2.5f);
          }
  
          if (PlayerShootingLazer == true)
          {
              target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
              RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, target, distanceRayCanSee + 50);
  
                  Debug.DrawLine(transform.position, target, Color.red);
                  playerLineOfSight.SetPosition(1, target);
                  playerLineOfSight.colorGradient = redColor;
  
              if (hitInfo.collider.CompareTag("Enemy"))
              {
                  print("YES");
                  Destroy(hitInfo.collider.gameObject);
                  hitInfo.collider.gameObject.SetActive(false);
              }
              else 
              {
                  Debug.DrawLine(transform.position, target * distanceRayCanSee, Color.red);
                  playerLineOfSight.SetPosition(1, target * distanceRayCanSee);
                  playerLineOfSight.colorGradient = redColor;
              }
  
              playerLineOfSight.SetPosition(0, transform.position);
          }
      }
  
      void OnTriggerEnter2D(Collider2D other)
      {
          shake.CamShake();
  
      }
  
      void EndLazer()
      {
          playerLineOfSight.enabled = false;
          PlayerShootingLazer = false;
      }
  }

If anyone has a solution to this, it would be extremely appreciated!

unnamed.png (98.1 kB)
unnamed-1.png (99.5 kB)
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 kskjadav007 · Dec 25, 2019 at 05:03 AM 0
Share

You can use Debug.Drwaray to see where raycast is shooting .

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_ek98vnTRplGj8Q · Dec 24, 2019 at 04:40 PM

Make sure you are feeding in a direction in your raycast. Right now you give it a start position and an end position, but you should be giving it a start position and a direction.

 Vector3 direction = target - transform.position;

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 ParyPro · Dec 25, 2019 at 02:50 AM 0
Share

Do you know at which part of my script I would make that change/ how to go about it?

avatar image unity_ek98vnTRplGj8Q ParyPro · Dec 26, 2019 at 04:04 PM 0
Share
 if (PlayerShootingLazer == true)
           {
               target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
               Vector2 direction = target - transform.position;
               RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, direction, distanceRayCanSee + 50);
   
avatar image
0

Answer by josephray1 · Jun 10, 2021 at 09:06 PM

I would recommend using LayerMasks to achieve what you're trying to do : this way you can save performances while being sure not to raycast undesired items. Also note you can use Debug.DrawRay() to display your raycast in Scene view.

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

169 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

Related Questions

My Raycasts seem to sometimes miss 0 Answers

What is the best way to raycast mutliple rays over a line 1 Answer

Get the object behind my gameobject by rayCast 1 Answer

2d Raycast not work 1 Answer

UI button and raycast script that I have made both aren't working to change my scene 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