I can’t make the hover function to work properly. I have a gameobject, with a rigidbody, charactercontroller, thirdpersoncamera, etc. All stuff seems to be very easy to fix but the damn hover script is not working! Here is the code I’m using:
I don’t know why the character cant fall on depressions and hover fixed X meters above the collision below. Seems that this AddForce method is doing nothing! If i force transform.position = ANYTHING works. But all hover tutorials says to use this damn AddForce.
It doesn’t seem like you’re using the distance from the ground at all here, you will want to use the RaycastHit.point and objects position to get the distance:
float d = Vector3.Distance(transform.position, hit.point);
or
float d = transform.position.y - hit.point.y;
rigidbody.AddForce ((Vector3.up + new Vector3 (0, d, 0)) * hoverForce);
Your hoverDistance variable is just the distance for the Raycast to travel
Oh and your link to the build doesn’t work, you can’t just give us a directory on your hard drive
Somehow, the code does not work either. This AddForce never works. If i force transform.position = someotherposition works, but adding force makes no effect. Any ideas why?
I think your high drag values may have been part of the problem, but I don’t know exactly what it is. I went and tried making a rigidbody hover, I had some trouble preventing it from ‘bouncing’ into the sky but I think I’ve got it figured out. I took the raycast’s hit position and used that to determine the target Y position, then took the players Y vs target Y to get the distance to multiply the force by. Since you should to be able to dip below the target height you have to limit the distance variable, otherwise you’ll fire off into the sky if you drop too low as the value will get really high. You could add a downwards force when you’re above it to counteract that but this should at least get you on the right track. It’ll work if you just throw this script on a cube, but you’ll want to change the drag and force limits, I just picked random values that show it works.
Yeah, I don’t think you’re supposed to use a character controller with a rigidbody, it’s meant as a replacement so you don’t have to use physics with varying friction/bounciness/etc. for your character. You might as well just use a rigidbody and capsule collider with the rotation locked, though you can probably achieve the same with the character controller using the Move function.
Sorry that I’m not sure what the exact topic at hand is, but I can affirm that the character controller is not meant to be used with rigidbody; it’s a substitution for moving/interacting with a character WITHOUT forces.