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;
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:
And here is my collider on the platform:
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
|
show 4 more comments
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:
And here is my collider on the platform:
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
1
What israyDistance
?
– Immersive
Mar 9 at 1:46
1
The length of yourDebug.DrawRay
isn't necessarily the same as yourPhysics.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. InPhysics.Raycast
it's in the maxDistance. InDebug.DrawRay
it's the length of the direction vector.
– Savaria
Mar 9 at 3:20
|
show 4 more comments
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:
And here is my collider on the platform:
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
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:
And here is my collider on the platform:
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
c# unity3d game-engine game-physics
asked Mar 9 at 1:28
UserDudeUserDude
486
486
1
What israyDistance
?
– Immersive
Mar 9 at 1:46
1
The length of yourDebug.DrawRay
isn't necessarily the same as yourPhysics.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. InPhysics.Raycast
it's in the maxDistance. InDebug.DrawRay
it's the length of the direction vector.
– Savaria
Mar 9 at 3:20
|
show 4 more comments
1
What israyDistance
?
– Immersive
Mar 9 at 1:46
1
The length of yourDebug.DrawRay
isn't necessarily the same as yourPhysics.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. InPhysics.Raycast
it's in the maxDistance. InDebug.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
|
show 4 more comments
1 Answer
1
active
oldest
votes
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. :)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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. :)
add a comment |
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. :)
add a comment |
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. :)
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. :)
edited Mar 9 at 13:26
answered Mar 9 at 12:45
FayFay
1124
1124
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
What is
rayDistance
?– Immersive
Mar 9 at 1:46
1
The length of your
Debug.DrawRay
isn't necessarily the same as yourPhysics.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. InDebug.DrawRay
it's the length of the direction vector.– Savaria
Mar 9 at 3:20