Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 legetic · Sep 07, 2015 at 03:46 AM · c#2draycastdetectionraycasthit2d

2d Raycast problem from enemy to player C#

The ray seems to detect the player but only when the player is standing "in" the enemy (they arent colliding).. i want the ray to trace to the player and to stop at the platform the player is standing on so that the player isn't "seen". The ray is going from the enemy to the "target" aka the player. All of this is in FixedUpdate. Thanks in advance / C# newbie

     Debug.DrawRay(myTransform.position, target.position - myTransform.position, Color.red); //debug ray to see the ray
     


     RaycastHit2D hit = Physics2D.Raycast(myTransform.position, target.position - myTransform.position);//from my transform in the direction to the player as seen in the debug ray
     


         if (hit.collider.tag == "Player")//if the object that was hit goes by the name "Player" then:
         {
             Debug.Log("HIT");//write HIT
         }

     if (hit.collider.tag == "SightBreak")//if the object that was hit goes by the name "SightBreak" then:
     {
         Debug.Log("cant see!!!!!!!!!!!");//write cant see!!!!!!!!!!!!!
     }
Comment
Add comment · Show 6
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 legetic · Sep 06, 2015 at 08:06 PM 0
Share

i have also put the platform in the layer "SightBreak" just in case.

avatar image legetic · Sep 07, 2015 at 09:50 AM 0
Share

Also worth noting may be that the platform collider turns of when the player is standing under it(so that the player may jump on to the platform from under.). Shouldn't be a problem though since the player i standing on the platform and therefore the platform collider is on.

avatar image Suddoha · Sep 07, 2015 at 11:10 AM 0
Share

Can you add some information what's the current behaviour with this script? Does it print anything at all? Do all objects have a 2d collider?

avatar image maccabbe · Sep 07, 2015 at 01:09 PM 0
Share

Try debugging to see what the ray hit ins$$anonymous$$d of what the ray should hit.

avatar image legetic · Sep 07, 2015 at 02:19 PM 0
Share

ok so i changed the debug to write what it collides with as you said and it now keeps printing "Enemy" until the player stands "in" the enemy where it then prints out a few "Player" over meanwhile "Enemy"is being printed. The goal is to make the enemy walk towards the player when the player is seen but that's not important right now. Right now i only need the ray to work correctly. Here's the whole code of the enemy.

     private Transform myTransform;//my (the enemy's)transform

 public Transform target;//The player't transform

 public int maxDistance;//detection distance

 public int moveSpeed = 3;//movement speed

 public int Direction;//direction of movement and sprite
 
 void Awake(){
     myTransform = transform;//init my transform
 }
 
 
 void Start () {
     GameObject go = GameObject.FindGameObjectWithTag("Player");//init player


     target = go.transform;//init player transform
     
     maxDistance = 10;//init detectiondistance
 }
 
 
 void Update () {

 


     if (target.position.x > myTransform.position.x +1) {
     
         Direction = 1;
     } else if(target.position.x < myTransform.position.x -1){
     
         Direction = -1;
     }

     
     if(Vector2.Distance(target.position, myTransform.position) < maxDistance){
         //$$anonymous$$ove towards target
     
         myTransform.position += myTransform.right * moveSpeed * Direction * Time.deltaTime;
         

     }

     gameObject.transform.localScale = new Vector2 (Direction, 1);


 }

 void FixedUpdate(){


     
     Debug.DrawRay(myTransform.position, target.position - myTransform.position, Color.red); //debug ray to see the ray
     


     RaycastHit2D hit = Physics2D.Raycast(myTransform.position, target.position - myTransform.position);//from my transform in the direction to the player as seen in the debug ray
     
     if (hit.collider != null)//if the object that was hit goes by the name "Player" then:
     {
         Debug.Log(hit.collider.tag);//write HIT
     }

         /*if (hit.collider.tag == "Player")//if the object that was hit goes by the name "Player" then:
         {
             Debug.Log("HIT");//write HIT
         }

     if (hit.collider.tag == "SightBreak")//if the object that was hit goes by the name "SightBreak" then:
     {
         Debug.Log("cant see!!!!!!!!!!!");//write HIT
     }*/


 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by legetic · Sep 07, 2015 at 03:49 PM

Thanks for the comments but i managed to solve it today, rest seems to have cleared my mind up hehe... I managed to figure out that the problem was that the ray collided with it self so i solved it with layermasking. This also meant i had to define a length of the ray but i just made it the distance between the enemy and player. My FixedUpdate:

     int layerMask = 1 << 9;
     layerMask = ~layerMask;
     
     Debug.DrawRay(myTransform.position, target.position - myTransform.position, Color.red); //debug ray to see the ray
     


     RaycastHit2D hit = Physics2D.Raycast(myTransform.position, target.position - myTransform.position,Vector2.Distance(target.position, myTransform.position),layerMask);//from my transform in the direction to the player as seen in the debug ray
     
     if (hit.collider != null)//if the object that was hit goes by the name "Player" then:
     {
         Debug.Log(hit.collider.tag);//write HIT
     }

Do i delete this question now since this might not help anyone?

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 holliebuckets ♦♦ · Sep 09, 2015 at 12:52 AM 0
Share

Hi!

Another way to solve this would be to use a Linecast ins$$anonymous$$d of a Raycast. I have a short (less than 5 $$anonymous$$s) tutorial on how to use a Linecast and a re$$anonymous$$der to make sure the linecast's starting position is outside the collider :D There is also a full transcript with screen shots on my blog :) One of the really powerful things about program$$anonymous$$g is are always a million different ways to do something ^_^

alt text

Tutorial video: https://youtu.be/e9TX6DEPfUQ Blog: http://lifeofbuckets.blogspot.com/2015/08/linecastsandColliders.html

2dlinecastandcolliders.png (167.0 kB)
avatar image legetic holliebuckets ♦♦ · Sep 09, 2015 at 08:15 AM 0
Share

Thank you!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast for a 2D objects 0 Answers

Raycasting in 2D is not working 0 Answers

2d raycast enemy detection problem. 0 Answers

Layer Mask on raycast2d not working? 1 Answer

Getting a Raycast rope to follow its object 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