Unity - raycast is clearly colliding but does not work?Unity: Object is not being detected by raycast for highlightingUnity Raycast problemsMoving perpendicular to an objectUnity Raycast always returning trueUnity: Raycast Doesn't Work For The Child Object's ColliderUnity: Raycast Doesn't Hit Child Object's ColliderRaycast 2d is not working in Unity3dRaycast based movement has weird interaction with collidersUnity Problem with Raycasting Masklayers or distanceChecking for Collisions with Raycasts - Collider not being registered when it clearly should be

Can I ask the recruiters in my resume to put the reason why I am rejected?

tikz convert color string to hex value

Client team has low performances and low technical skills: we always fix their work and now they stop collaborate with us. How to solve?

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?

Replacing matching entries in one column of a file by another column from a different file

Is there a name for fork-protected pieces?

Languages that we cannot (dis)prove to be Context-Free

Modeling an IP Address

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

How much RAM could one put in a typical 80386 setup?

How old can references or sources in a thesis be?

LWC SFDX source push error TypeError: LWC1009: decl.moveTo is not a function

Can a vampire attack twice with their claws using Multiattack?

Find the result of this dual key cipher

Is it possible to do 50 km distance without any previous training?

Is it unprofessional to ask if a job posting on GlassDoor is real?

Filter any system log file by date or date range

Why can't we play rap on piano?

What does the "remote control" for a QF-4 look like?

What's the output of a record needle playing an out-of-speed record

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

High voltage LED indicator 40-1000 VDC without additional power supply

strTok function (thread safe, supports empty tokens, doesn't change string)

What typically incentivizes a professor to change jobs to a lower ranking university?



Unity - raycast is clearly colliding but does not work?


Unity: Object is not being detected by raycast for highlightingUnity Raycast problemsMoving perpendicular to an objectUnity Raycast always returning trueUnity: Raycast Doesn't Work For The Child Object's ColliderUnity: Raycast Doesn't Hit Child Object's ColliderRaycast 2d is not working in Unity3dRaycast based movement has weird interaction with collidersUnity Problem with Raycasting Masklayers or distanceChecking for Collisions with Raycasts - Collider not being registered when it clearly should be






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















Ok I have 2 objects on the Default layer that I need to trigger a jump with when raycast collides. I can see that the raycast is intersecting the platform:



enter image description here



And here is my collider on the platform:



enter image description here



And yet nothing is printed with:



Vector3 rotation = transform.forward;

RaycastHit hit;
Debug.DrawRay(new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z), rotation, Color.green);
if (Physics.Raycast(new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z), rotation, out hit, rayDistance))

print(hit.transform);
if (hit.transform.GetComponent<Platform>() != null)

Jump(hit.transform);




What is wrong here?










share|improve this question

















  • 1





    What is rayDistance?

    – Immersive
    Mar 9 at 1:46






  • 1





    The length of your Debug.DrawRay isn't necessarily the same as your Physics.RayCast. Make sure they are actually colliding

    – Savaria
    Mar 9 at 2:05











  • @Savaria what do you mean? How can I see what the actual raycast is or if they are colliding?

    – UserDude
    Mar 9 at 3:12











  • @Immersive 1.2f

    – UserDude
    Mar 9 at 3:12











  • The length of your ray is stored in different variables in the two functions. In Physics.Raycast it's in the maxDistance. In Debug.DrawRay it's the length of the direction vector.

    – Savaria
    Mar 9 at 3:20

















1















Ok I have 2 objects on the Default layer that I need to trigger a jump with when raycast collides. I can see that the raycast is intersecting the platform:



enter image description here



And here is my collider on the platform:



enter image description here



And yet nothing is printed with:



Vector3 rotation = transform.forward;

RaycastHit hit;
Debug.DrawRay(new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z), rotation, Color.green);
if (Physics.Raycast(new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z), rotation, out hit, rayDistance))

print(hit.transform);
if (hit.transform.GetComponent<Platform>() != null)

Jump(hit.transform);




What is wrong here?










share|improve this question

















  • 1





    What is rayDistance?

    – Immersive
    Mar 9 at 1:46






  • 1





    The length of your Debug.DrawRay isn't necessarily the same as your Physics.RayCast. Make sure they are actually colliding

    – Savaria
    Mar 9 at 2:05











  • @Savaria what do you mean? How can I see what the actual raycast is or if they are colliding?

    – UserDude
    Mar 9 at 3:12











  • @Immersive 1.2f

    – UserDude
    Mar 9 at 3:12











  • The length of your ray is stored in different variables in the two functions. In Physics.Raycast it's in the maxDistance. In Debug.DrawRay it's the length of the direction vector.

    – Savaria
    Mar 9 at 3:20













1












1








1








Ok I have 2 objects on the Default layer that I need to trigger a jump with when raycast collides. I can see that the raycast is intersecting the platform:



enter image description here



And here is my collider on the platform:



enter image description here



And yet nothing is printed with:



Vector3 rotation = transform.forward;

RaycastHit hit;
Debug.DrawRay(new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z), rotation, Color.green);
if (Physics.Raycast(new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z), rotation, out hit, rayDistance))

print(hit.transform);
if (hit.transform.GetComponent<Platform>() != null)

Jump(hit.transform);




What is wrong here?










share|improve this question














Ok I have 2 objects on the Default layer that I need to trigger a jump with when raycast collides. I can see that the raycast is intersecting the platform:



enter image description here



And here is my collider on the platform:



enter image description here



And yet nothing is printed with:



Vector3 rotation = transform.forward;

RaycastHit hit;
Debug.DrawRay(new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z), rotation, Color.green);
if (Physics.Raycast(new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z), rotation, out hit, rayDistance))

print(hit.transform);
if (hit.transform.GetComponent<Platform>() != null)

Jump(hit.transform);




What is wrong here?







c# unity3d game-engine game-physics






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 9 at 1:28









UserDudeUserDude

486




486







  • 1





    What is rayDistance?

    – Immersive
    Mar 9 at 1:46






  • 1





    The length of your Debug.DrawRay isn't necessarily the same as your Physics.RayCast. Make sure they are actually colliding

    – Savaria
    Mar 9 at 2:05











  • @Savaria what do you mean? How can I see what the actual raycast is or if they are colliding?

    – UserDude
    Mar 9 at 3:12











  • @Immersive 1.2f

    – UserDude
    Mar 9 at 3:12











  • The length of your ray is stored in different variables in the two functions. In Physics.Raycast it's in the maxDistance. In Debug.DrawRay it's the length of the direction vector.

    – Savaria
    Mar 9 at 3:20












  • 1





    What is rayDistance?

    – Immersive
    Mar 9 at 1:46






  • 1





    The length of your Debug.DrawRay isn't necessarily the same as your Physics.RayCast. Make sure they are actually colliding

    – Savaria
    Mar 9 at 2:05











  • @Savaria what do you mean? How can I see what the actual raycast is or if they are colliding?

    – UserDude
    Mar 9 at 3:12











  • @Immersive 1.2f

    – UserDude
    Mar 9 at 3:12











  • The length of your ray is stored in different variables in the two functions. In Physics.Raycast it's in the maxDistance. In Debug.DrawRay it's the length of the direction vector.

    – Savaria
    Mar 9 at 3:20







1




1





What is rayDistance?

– Immersive
Mar 9 at 1:46





What is rayDistance?

– Immersive
Mar 9 at 1:46




1




1





The length of your Debug.DrawRay isn't necessarily the same as your Physics.RayCast. Make sure they are actually colliding

– Savaria
Mar 9 at 2:05





The length of your Debug.DrawRay isn't necessarily the same as your Physics.RayCast. Make sure they are actually colliding

– Savaria
Mar 9 at 2:05













@Savaria what do you mean? How can I see what the actual raycast is or if they are colliding?

– UserDude
Mar 9 at 3:12





@Savaria what do you mean? How can I see what the actual raycast is or if they are colliding?

– UserDude
Mar 9 at 3:12













@Immersive 1.2f

– UserDude
Mar 9 at 3:12





@Immersive 1.2f

– UserDude
Mar 9 at 3:12













The length of your ray is stored in different variables in the two functions. In Physics.Raycast it's in the maxDistance. In Debug.DrawRay it's the length of the direction vector.

– Savaria
Mar 9 at 3:20





The length of your ray is stored in different variables in the two functions. In Physics.Raycast it's in the maxDistance. In Debug.DrawRay it's the length of the direction vector.

– Savaria
Mar 9 at 3:20












1 Answer
1






active

oldest

votes


















2














Try this code adapted from https://docs.unity3d.com/ScriptReference/Physics.Raycast.html using your values:



RaycastHit hit;
Vector3 startPoint = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
if (Physics.Raycast(startPoint, rotation, out hit, Mathf.Infinity))

Debug.DrawRay(startPoint, rotation * hit.distance, Color.yellow);
Debug.Log("Did Hit");

else

Debug.DrawRay(startPoint, rotation * 1000, Color.white);
Debug.Log("Did not Hit");



Here it is a GIF from a test I did.



This is just a test to see if the problem is the difference between ray distances.



You may also use Debug.DrawLine instead and then set up the end point with the same distance used in the RayCast.



Vector3 startPoint = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
if (Physics.Raycast(startPoint, rotation, out hit, rayDistance))

Debug.DrawLine(startPoint, startPoint + rotation * rayDistance, Color.green);

else Debug.DrawLine(startPoint, startPoint + rotation * rayDistance, Color.red);


Another GIF using this code.



If it still doesn't work, try checking out the matrix collision.



Don't forget that you may also use a layermask in your raycast. :)






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073121%2funity-raycast-is-clearly-colliding-but-does-not-work%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Try this code adapted from https://docs.unity3d.com/ScriptReference/Physics.Raycast.html using your values:



    RaycastHit hit;
    Vector3 startPoint = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
    if (Physics.Raycast(startPoint, rotation, out hit, Mathf.Infinity))

    Debug.DrawRay(startPoint, rotation * hit.distance, Color.yellow);
    Debug.Log("Did Hit");

    else

    Debug.DrawRay(startPoint, rotation * 1000, Color.white);
    Debug.Log("Did not Hit");



    Here it is a GIF from a test I did.



    This is just a test to see if the problem is the difference between ray distances.



    You may also use Debug.DrawLine instead and then set up the end point with the same distance used in the RayCast.



    Vector3 startPoint = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
    if (Physics.Raycast(startPoint, rotation, out hit, rayDistance))

    Debug.DrawLine(startPoint, startPoint + rotation * rayDistance, Color.green);

    else Debug.DrawLine(startPoint, startPoint + rotation * rayDistance, Color.red);


    Another GIF using this code.



    If it still doesn't work, try checking out the matrix collision.



    Don't forget that you may also use a layermask in your raycast. :)






    share|improve this answer





























      2














      Try this code adapted from https://docs.unity3d.com/ScriptReference/Physics.Raycast.html using your values:



      RaycastHit hit;
      Vector3 startPoint = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
      if (Physics.Raycast(startPoint, rotation, out hit, Mathf.Infinity))

      Debug.DrawRay(startPoint, rotation * hit.distance, Color.yellow);
      Debug.Log("Did Hit");

      else

      Debug.DrawRay(startPoint, rotation * 1000, Color.white);
      Debug.Log("Did not Hit");



      Here it is a GIF from a test I did.



      This is just a test to see if the problem is the difference between ray distances.



      You may also use Debug.DrawLine instead and then set up the end point with the same distance used in the RayCast.



      Vector3 startPoint = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
      if (Physics.Raycast(startPoint, rotation, out hit, rayDistance))

      Debug.DrawLine(startPoint, startPoint + rotation * rayDistance, Color.green);

      else Debug.DrawLine(startPoint, startPoint + rotation * rayDistance, Color.red);


      Another GIF using this code.



      If it still doesn't work, try checking out the matrix collision.



      Don't forget that you may also use a layermask in your raycast. :)






      share|improve this answer



























        2












        2








        2







        Try this code adapted from https://docs.unity3d.com/ScriptReference/Physics.Raycast.html using your values:



        RaycastHit hit;
        Vector3 startPoint = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
        if (Physics.Raycast(startPoint, rotation, out hit, Mathf.Infinity))

        Debug.DrawRay(startPoint, rotation * hit.distance, Color.yellow);
        Debug.Log("Did Hit");

        else

        Debug.DrawRay(startPoint, rotation * 1000, Color.white);
        Debug.Log("Did not Hit");



        Here it is a GIF from a test I did.



        This is just a test to see if the problem is the difference between ray distances.



        You may also use Debug.DrawLine instead and then set up the end point with the same distance used in the RayCast.



        Vector3 startPoint = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
        if (Physics.Raycast(startPoint, rotation, out hit, rayDistance))

        Debug.DrawLine(startPoint, startPoint + rotation * rayDistance, Color.green);

        else Debug.DrawLine(startPoint, startPoint + rotation * rayDistance, Color.red);


        Another GIF using this code.



        If it still doesn't work, try checking out the matrix collision.



        Don't forget that you may also use a layermask in your raycast. :)






        share|improve this answer















        Try this code adapted from https://docs.unity3d.com/ScriptReference/Physics.Raycast.html using your values:



        RaycastHit hit;
        Vector3 startPoint = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
        if (Physics.Raycast(startPoint, rotation, out hit, Mathf.Infinity))

        Debug.DrawRay(startPoint, rotation * hit.distance, Color.yellow);
        Debug.Log("Did Hit");

        else

        Debug.DrawRay(startPoint, rotation * 1000, Color.white);
        Debug.Log("Did not Hit");



        Here it is a GIF from a test I did.



        This is just a test to see if the problem is the difference between ray distances.



        You may also use Debug.DrawLine instead and then set up the end point with the same distance used in the RayCast.



        Vector3 startPoint = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
        if (Physics.Raycast(startPoint, rotation, out hit, rayDistance))

        Debug.DrawLine(startPoint, startPoint + rotation * rayDistance, Color.green);

        else Debug.DrawLine(startPoint, startPoint + rotation * rayDistance, Color.red);


        Another GIF using this code.



        If it still doesn't work, try checking out the matrix collision.



        Don't forget that you may also use a layermask in your raycast. :)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 9 at 13:26

























        answered Mar 9 at 12:45









        FayFay

        1124




        1124





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073121%2funity-raycast-is-clearly-colliding-but-does-not-work%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme