I designed my collisions by sending out a raycast straight down and gathering the hit point value to compare with the transform of the object. The issue I am running into is that if the ray cast hit fails to hit a target, it's output becomes 0 which when compared to the object transform would then set the gameobject to 0 in world space. The fix I came up with is: if raycast == false groundValue = - 1000; This works but setting the value like that seems wrong. Any techniques for this scenario? ---Edit This has lead to other issues. Reworking.
Posting the code would make it alot easier to help, but from what you have said i think doing something like this will help: Code (CSharp): If(Physics.Raycast(origin, dir, out hit, dist)){ //your hit code in here }
I understand how to raycast, the issue is that I am comparing the players transform location with the hit point data to detect the ground collision. If no hit is detected, the comparison value becomes 0, so the player transform will stop when it hits Y value of 0 in world space.
Here is the script after a few fixes, now works as I want it but that value still bugs me. preCal i use to precalculate the position of the transform on the next frame. If the preCal goes below the value of the hitpoint value then it will snap the transform down to the surface(this is to fix the animation problem of snapping up on the following frame if I compared it to the current value). Line 7 is the issue Code (CSharp): castGround (); groundVal = floorHit.point.y; if(gCast == false) { groundVal = -10000.0f; gravityToggle = true; } if(gravityToggle == true) { gravityVal = trueGravity; applyGravity(); } if (pre_CalFrame.y < groundVal) { transform.position = new Vector3(transform.position.x, groundVal, transform.position.z); gravityToggle = false; } if (pre_CalFrame.y > groundVal) { gravityToggle = true; }