• 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 /
  • Help Room /
avatar image
0
Question by CheeseChipMansion · Mar 07, 2019 at 08:32 AM · collisionraycastcolliderraycasthit2dcast

Raycast collisions being detected along a non-existent curve(?)

I have written a brief amount of code for debugging purposes to show visually when the player is on solid ground or not. The code can be seen here:

 private void UpdateCastValues()
     {
         //Casting
         Ray2DBegin = ((Vector2)transform.position);
         Ray2DEnd = ((Vector2)this.gameObject.transform.position + (Vector2.down * 0.75f));
     }
 
 private bool IsPlayerGrounded()
     {
 
         try
         {
             if (CheckHit().collider.tag == "ground")
                 return true;
             else
                 return false;
         }
         catch
         {
             return false;
         }
     }
 
 private RaycastHit2D CheckHit()
     { 
         hit = Physics2D.Raycast(Ray2DBegin, Ray2DEnd);
 
         return hit;
     }
 
 private void OnDrawGizmos()
     {
         if (IsPlayerGrounded())
             Gizmos.color = Color.green;
         else
             Gizmos.color = Color.red;
 
 
         Gizmos.DrawLine(Ray2DBegin, Ray2DEnd);
     }

Gizmo

Here we can see the player, which has a square collision box. Below the player is a Tilemap with a TilemapCollider2D component.

As you can see in the above image, when the player moves to the left, the gizmo turns red, indicating that there is no "ground" collision below the player, however, the player remains moving horizontally across the ground, as the relationship between the player's collision detection and the ground registers as a perfectly straight line.

The reason for this is because the collision for the Tilemap below is registering along a curve. I will show this in the picture below.

alt text

If you will excuse the shoddy drawing, by moving the player around and checking when the gizmo goes from red to green, I have identified a curvature being detected basically where I have drawn. It it goes from below, to higher up, and then below the tilemap.

This makes no sense to me. This is a first game for me so any help is appreciated.

capture.png (41.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
1
Best Answer

Answer by xxmariofer · Mar 07, 2019 at 09:11 AM

hello your problem is understanding the raycast. the ray2d end is the direction, not a position where the raycast ends, so you dont need to multiply * 0.75 (since is just a direction not a length) and dont have to add the player position. override the raycast line with this

 hit = Physics2D.Raycast(Ray2DBegin, Vector2.down, 0.75f);//you need to pass the lenbgth of the ray as argument

also good job explaining your issue and giving all the debug information, and the code structure, was easy to understand :)

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.

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

264 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

CircleCast hit its origin... 0 Answers

Create a custom polygon collider based on overlapping ogjects 0 Answers

How to get my `collider2D.cast` work? 1 Answer

Make raycast ignore hitbox? 0 Answers

How do I do a spherecast/boxcast/whatever but have the radius increase proportionally with distance? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges