- Home /
Debug.Log(Vector3.Angle(ray.direction,hit.normal)); HELP!
OK IM GOING ABSOLUTELY CRAZY HERE!!!
I have a ball rolling on a floor. The floor is perfectly flat. I use Vector3.Angle(ray.direction,hit.normal) to get an angle of the ball to the floor. When the floor is perfectly flat the angle returned is 180 degrees. This occurs when the ball is still and when it is rolling.
NOW HERES THE WEIRDNESS: On some levels the angle stays at 180 without changing at all. On other levels though, when the ball is rolling around the floor, the angle changes slightly by a few digits. WHY IS THIS HAPPENING I HAVE NO EXPLANATION!?!?
The crazy part is, i use a floor prefab which i tried placing on a level while it's running. If it was a level where the angle returned was always 180, then the new floor angle would also return 180 when the ball rolled over it. But when i put the floor prefab in a level where the angle changed while the ball rolled then the new floor would produce the same effect EVEN THOUGH ITS THE SAME EXACT PREFAB USED FOR BOTH LEVELS!!!
Please help, im really at a loss here... thanks.
EDIT: Okay tried doing Debug.Log(hit.normal) to see if that changes on different floors and it doesnt. The value returned is always (0.0,1.0,0.0) on FLAT, NON ANGLED surfaces. This happens on every single level.
It stands to reason then that it's the ray.direction that causing the problem, but i dont understand how considering im using the same exact script for all my levels?
EDIT 2: OK EVEN MORE WEIRDNESS. If i create a brand new scene with just a ball, floor, camera, light. When i run it multiple times, the angle is 180 all the time. BUT WHEN I CHANGE TO ANOTHER SCENE AND CHANGE BACK TO THAT SCENE THE ANGLE STARTS CHANGING!!! what could possibly be happening?!?!?
EDIT 3: OKAY so it all boils down to this... If i delete the ball in any scene and replace it with a new prefab ball then the angle stays 180 degrees no matter how many times i run it. I save and load a different scene, and then reload the previous one again. And the problem comes back. WHY????
How much is 'a few digits'? What is ray.direction (better yet, what code is running it)?
the ray is: ray = new Ray(shadowProjector.transform.position + shadowProjector.transform.forward.normalized * origNearClipPlane, shadowProjector.transform.forward);
It runs in Update()
Basically the ray.direction is supposed to be (0.0,-1.0,0.0) but when it messes up, it swings based on the direction of the ball rolling, like this (0.1,-0.9,0.0)...
http://www.youtube.com/watch?v=xVoNgLQHSYA&feature=youtu.be
The green line is the ray. As you can see when i place a new prefab ball, the ray is absolutely straight down, but when i reload the scene after saving it of course, the problem returns. This makes me think that its a bug...
the angle is off by a few degrees(around 3-10) depending on the rolling. I basically use Vector.Angle() to deter$$anonymous$$e my shadowDistanceTolerance which is no related to the problem.
Answer by aldonaletto · Nov 17, 2012 at 03:24 PM
If shadowProjector refers to a separate object which follows the sphere in Update, the problem may be caused by different script execution order: when the projector Update executes before the sphere's one, it follows an outdated sphere position and cause this weird behaviour (looks exactly like in your video). Deleting and adding objects may affect the order scripts are executed, what could explain why it gets screwed up after deleting and recreating the sphere.
If this is the problem, move the projector code that follows the sphere from Update to LateUpdate, so that it will always execute after the sphere has moved in the frame.
Thank you sir, i have set the ray code to LateUpdate() and that worked. Amazing...