Search Unity

Question How to collider cast in a direction until hitting an object?

Discussion in '2D' started by Max-0, Jun 4, 2023.

  1. Max-0

    Max-0

    Joined:
    Jun 10, 2021
    Posts:
    31
    Example: How can I cast my object to the right until it detects another object, and right when it touches it, it returns the position it's in?
    I tried Collider2D.Cast() but it returns the point where it hit. I need to have the place where the casted object is, right when it touches another object.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Start with some basic casting tutorials. No sense one of us trying to retype it all.

    Also, read specifically in the docs for what that particular method does and all the requirements for it to function.

    Otherwise, this smells like an XY Problem, so perhaps you should start with what you are trying to do.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    Don't TALK about code without posting it. Don't RETYPE code. Copy/paste and post code properly. ONLY post the relevant code, and then refer to it in your discussion.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,450
    In the very least, you should always look at the scripting docs to see what's available. In your case you can see that RaycastHit2D.centroid is what you need.