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 madruytb · May 10, 2018 at 01:37 AM · raycastplatformerlinecast

Linecast always returning true

I am trying to make a linecast to avoid my enemy to fall off the map and getting stuck on walls, if he detects an edge or wall, he flips and start moving the other way. The linecast to detect edges is doing just fine, however the linecast facing left (to detect walls), is always getting triggered and i dont know why, so the enemy just keeps flipping every frame.

 public class Enemy1 : MonoBehaviour {
 
     Rigidbody2D myBody;
     Transform myTransf;
     public LayerMask enemyMask;
 
     float myWidth;
     public float speed;
 
     // Use this for initialization
     void Start () {
         myTransf = this.transform;
         myBody = this.GetComponent<Rigidbody2D>();
         myWidth = this.GetComponent<SpriteRenderer>().bounds.extents.x;
 
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         //Checking if theres an edge or wall, if so, flip and move the other way.
         Vector2 lineCastPos = myTransf.position - myTransf.right * myWidth;
         Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
         bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);
         Debug.DrawLine(lineCastPos, lineCastPos - myTransf.right.toVector2() *0.1f);
         bool isBlocked = Physics2D.Linecast(lineCastPos, lineCastPos - myTransf.right.toVector2() * 0.1f, enemyMask);
         RaycastHit2D hit = Physics2D.Linecast(lineCastPos, lineCastPos - myTransf.right.toVector2());
 
         if (!isGrounded || !isBlocked)
             {
             Vector3 currRot = myTransf.eulerAngles;
             currRot.y += 180;
             myTransf.eulerAngles = currRot;
             Debug.Log("Ray Hit: " + hit.point);
         }
 
         //Always walking to the left
         Vector2 myVel = myBody.velocity;
         myVel.x = -myTransf.right.x * speed;
         myBody.velocity = myVel;
         
     }
 }

The RayCastHit2D Log gives me the point 0,0.0,0 (highlighted in the red dot in my screenshot) and nothing is sitting there, only the main camera. alt text

excluir53.jpg (17.5 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rd_mcn · May 10, 2018 at 03:43 AM

Hi,

Not sure if this may help but my Raycasts were initially hitting the game object that was originating the cast and therefore always returning a hit. I found this by checking the game object name of the collider that returns the hit. Hope this might help.

Comment
Add comment · Show 1 · 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 madruytb · May 10, 2018 at 05:01 AM 0
Share

Hey, Thanks for the input, but I think I already discarded that option, before I created this post, I've found this page talking exactly about this issue ( https://stackoverflow.com/questions/38191659/unity-physics2d-raycast-always-returns-the-invoker-as-hit/38193001#38193001 ) I already tried both methods shown to prevent this issue. Plus as an indicator, the Debug.Log("Ray Hit: " + hit.point); doesnt return the same position as the linecast invoker (in this case the position registered is 0.00, 0.00)

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

122 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

Related Questions

2D Slope acceleration 1 Answer

Issues with Linecast/Raycast 1 Answer

Simple wire/cable 1 Answer

Need to find a point along a linecast slightly closer to the origin than hit.point 1 Answer

Raycast, Linecast, sphere collider or capsule collider? 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