- Home /
Physics.SphereCast depends on Debug.DrawRay
Hi,
I'm working on a little game with the ball in labyrinth. I decided to make my own physics for the ball. The problem is the detection of a collision with the ground. At first I just used Physics.Raycast, but I have a place where ray does not detect a collision. So I decided to use Physics.SphereCast. But it is not working properly. If you set maxDistance normal, it does not detect a collision, if more, my ball just fly. Now I decided to use Debug.DrawRay, to watch when SphereCast hits to the ground. The most unexpected is that SphereCast started working properly. However, when I remove DrawRay function, SphereCast again begins to make mistakes. So can anybody help me with this? Using of DrawRay function is not what I want. :(
There can be mistakes, because my English is not very good, and I used Google Translate, so sorry.
That's part of the code on C# that I use.
float radius = GetComponent ().radius * transform.lossyScale.y;
Vector3 direction = Vector3.up * gravityPower / 10; //gravityPower = -10, but it can be +10
Debug.DrawRay (transform.position, direction * radius, Color.red, 3);
if (Physics.SphereCast (new Ray (transform.position, direction), radius, radius * 0.1F))
velocity.y = 0;
else
velocity.y += gravityPower / mass * Time.fixedDeltaTime;
Okay, I found that if I will move the start of SphereCast in the opposite direction, then it will work. However, I still wonder why DrawRay have so much influence on the result.
I tested out your code and it works for me.
Your answer
Follow this Question
Related Questions
Physics.Raycasy not really using Layermask? 1 Answer
Having a RaycastHit event create a component? 1 Answer
thousands of raycasts in nested for loop 0 Answers
Trouble with checking Raycast 1 Answer
Physics2D.Raycast Reflect issue 1 Answer