Why isnt my raycast hitting the floor?

Background. Im making a hovercar game, and I am using raycasts to add an upwards force which lessens the further from the ground the car gets, giving a smooth, bouncy effect.

I would however like the raycast and forces to come out at an angle, to stablize the car more in the upright position ( think negative camber ).

The code orginially had the forces and raycasts casting directly down, however i attempted to change that as follows.

 Rigidbody m_body;
public float m_hoverForce = 9.0f;
public float m_hoverHeight = 2.0f;
public GameObject[] m_hoverPoints;
int m_layerMask;
// Use this for initialization
void Start () {
     m_body = GetComponent<Rigidbody>();
     m_layerMask = 1 << LayerMask.NameToLayer("Characters");
     m_layerMask = ~m_layerMask;
}
// Update is called once per frame
void FixedUpdate () {
     RaycastHit hit;
     for (int i = 0; i < m_hoverPoints.Length; i++)
     {
         var hoverPoint = m_hoverPoints [i];
         if (Physics.Raycast(hoverPoint.transform.position, hoverPoint.transform.forward , out hit, m_hoverHeight, m_layerMask))
             m_body.AddForceAtPosition
             (hoverPoint.transform.forward * m_hoverForce * (1.0f - (hit.distance / m_hoverHeight)), hoverPoint.transform.position);

Im not the best at coding and have just started out. Im using a script i learned from a tutorial and im trying to change it so that my raycasts come out in the direction of the hover points (locally) rather than just straight down globally.

The code previously had -Vector3.up instead of hoverPoint.transform.forward, in the if statement parameters.

While debuging, I see the raycast comes out perfecly as I want it, however for some reason it is not registering a hit with the floor, however it registers the hit when I use -Vector3.up (which isnt what I want)

Does anyone know why this could be happing? All answers appreicated!

Is the raycast hitting anything? IF it is, print the name of it out. It might be your vehicle, in which case you want to use Layers to select what you want to hit.

Also, you can use Gizmos.DrawRay() to show the ray in real time in the editor, to make sure it really is where you think it is.

Yeah im drawing the ray too, and no its not hitting anything, going through the floor. It hits the floor fine when its -Vector3.up though.

Edit: Fixed. Code should have been -hoverPoint.transform.forward

1 Like

You total muppet