• 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 Alphacreations · Jun 28, 2020 at 02:17 AM · raycastunity 2ddistance

How to use raycasting to find distance between enemy and obstacle

So I have an enemy and I want it to jump over obstacles that are between it and the player. I already have it following the player but it can't jump. How do I use raycasts to find the distance from the enemy and the obstacle?

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 kylecat6330 · Jun 30, 2020 at 12:00 AM

It sounds like you just need the distance between the enemy and the obstacle in order to know when the enemy needs jump so it can get over it. If that is the case then you shouldn't really need the distance at all. You can simply make the raycast only extend a set distance and when it hits an obstacle the enemy jumps. You will also probably want to make it so that this raycast only picks up on obstacles, so that the enemy doesn't jump when it gets close to the player, a wall, or anything like that. For that you'll need to give all the obstacles a tag. Then you can use a code like this.

 RaycastHit2D hit = Physics2D.Raycast(enemy.transform.position, DirectionEnemyIsMovingIn, DesiredDistanceBetweenEnemyAndObstacle)
 if (hit.transform.tag == "ObstacleTag")
 {
      MakeEnemyJump;
 }

Of course you need to take into account the height of the obstacles. The code above has the raycast extending out from the middle of the enemy if the obstacle is below that then you'll need to change the enemy.transform.position vector so that it is lower.

Now if you actually need the distance between the enemy and obstacle then just know that hit.distance is the distance between the ray's origin (enemy.transform.position in the example) and the point that the raycast hit the obstacle.

I haven't tested this and I am still pretty new to working in 2D in opposed to 3D, so its possible I messed up somewhere, but it looks like it should work. I hope this helps and let me know if it didn't.

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

185 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

Related Questions

Get the distance between the origin and HitPoint for Raycast? 1 Answer

How to get co-ordinates of end point of Raycast2D ? 1 Answer

Is Raycast relatived to screen resolution? 1 Answer

Math - calculate position in world space from ray on infinite plane 2 Answers

Picking up rigidbody objects 2 Answers

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