• 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 MomkeyDev · Nov 03, 2020 at 04:03 PM · nullreferenceexceptionraycastingraycasthit2d

Raycast2D hit.point is zero ?

So what im trying to do is making my character climb a ledge, but when trying to make a logic where i put my player snap to near the ledge/wall. the hit.point keep returning zero. i do found that the hit.collider return null, thought it did hit something and i don't know why hit.point is zero. any answer will be really appreciated

 //what im trying to do in this script is basically snapping my player near the ledge
 //when they want to climb, but the hit.point only return zero vector
 
 float LedgeRayDistance = 1f; //The Distance for ray
 
 bool LGU;               //BOOL TO CHECK THE TOP OF PLAYER
 bool LGM;               //BOOL TO CHECK THE MIDDLE PART
 bool LGD;               //BOOL TO CHECK THE BOTTOM PART
 
 Vector2 LRay2;          //The offset for the bottom ray
 Vector2 LRay3;          //The offset for the middle ray (Sorry for weird numbering)
 
 LayerMask WhatIsLedge;  //The layer mask to seperate the player at first
 
 //what this if basically do is to check if both the LGM and LGD is true and LGU is false
 //which basically mean they can climb
 if (!LedgeCheckUp && LedgeCheckDown | LedgeCheckMiddle)
 {
     //Change the transform to near the ledge
     if (LedgeCheckDown)             //If it's on the bottom part that got true
     {
         //this next logic basically calculate the distance between the hit.point and
         //the player position + player collider size in the x (so it doesn't overlap)
 
         Vector2 Ledge2 = (Vector2)transform.position + LRay2;       //calculate the 
         RaycastHit2D hit = Physics2D.Raycast(Ledge2, Vector2.right * transform.localScale.x, LedgeRayDistance);
                 
         //Where the problem start, i don't know why but the hit.point is returning zero
         Vector2 Ledgepos = hit.point - (Vector2.right * GetComponent<BoxCollider2D>().size.x * transform.localScale.x);
         transform.position = Ledgepos;
     }
     else if (LedgeCheckMiddle)      //if the middle part that got true
     {
         //The same as above. not really important
 
         Vector2 Ledge3 = (Vector2)transform.position + LedgeRay3;
         RaycastHit2D hit = Physics2D.Raycast(Ledge3, Vector2.right * transform.localScale.x);
         Vector2 Ledgepos = hit.point - (Vector2.right * GetComponent<BoxCollider2D>().size.x * transform.localScale.x);
         transform.position = Ledgepos;
     }
 }
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

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.

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

219 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

Related Questions

2D raycast not working 0 Answers

Vertical Rays rapidly changing between "detecting ground" to "not detecting ground" 0 Answers

2D raycasting in jave script isnt hitting collider 0 Answers

Select from multiple character to move 0 Answers

NullReferenceException on Raycast 0 Answers

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