- Home /
Maintain height/Altitude/distance from ground?
Hi, How can i make something maintain a certain distance between the ground and the player? ( something like a hovercraft ) I tried raycasting below the object and adding force to it when its lower than a certain distance, but it looks odd with the bumping and its not very smooth since i wanted to move the object around, like a racing game. THANKS!
Answer by PrimeDerektive · Feb 03, 2011 at 04:26 PM
I would probably do what you mentioned in your original answer. Did you do it right? I guess I would do something like this (not tested):
var height: float = 5.0; var forceMultiplier : float = 10.0;
function FixedUpdate(){
var hit : RaycastHit;
if(Physics.Raycast(transform.position, -transform.up, hit, height)){
rigidbody.AddForce(transform.up*(forceMultiplier/(hit.distance/2)));
}
}
Some Notes:
1) I'm using FixedUpdate(), don't forget to use FixedUpdate() for applying rigidbody forces.
2) Make sure you set freeze rotation on the rigidbody.
3) By dividing forceMultipler by (hit.distance/2), I am attempting to apply a force relative to the length of the ray (the height off the ground). So the lower the hovercraft goes, the more upward force is applied to balance it out. This was just a crappy theory, I'm no math whiz, but I played around with some different numbers and this looks pretty good. You could try tweaking it or just changing the formula and see what happens.
Hmm, I think you're pretty much relegated to doing what you mentioned in your original post. How did you do it though? See my updated answer as to how I would probably do it.
Answer by Senhor de todo o Mal · Feb 03, 2011 at 03:39 PM
There are two ways I can see to do this.
You can either add a character controller that extends a bit downward, then use Move and/or SimpleMove to move around.
Or you can add a collider, set it as trigger and add upwards force if it hits the ground:
function OnTriggerEnter(other:Collider){ // if other == floor start applying upwards force }
function OnTriggerEnter(other:Collider){ // if other == floor stop applying upwards force }
i didnt want the hovercraft to just " appear " floating by using a collider, i wanted the controller to lift and move dynamically according to the terrain/area condition,(eg : lifting from a lower ground to a higher stair ) not faking it.
I'm not quite understanding. If you want it to bob, the trigger collider will do just fine, it's just a trigger with no collisions, when it is triggered by the floor you add upwards force, there won't be a snapping effect, it will give a sort of bobbing motion.
Your answer
Follow this Question
Related Questions
Comparing values of an array 1 Answer
how to display distance in scene 2 Answers
Should i use terrain generation or make it myself for android? 0 Answers
Script Conversion JS to C# 3 Answers