- Home /
Hovering script, set deceleration
Hi, i'm trying to make a vehicle hover a certain height above the ground, I've either made or found this script ( i haven't worked on this for a while), which does sort of work but is not ideal as it just bounces around far too much
What i'm really trying to do is make a script which detects how far you are from the ground, and sets a deceleration for that, however i've only just started to learn js so how would you do this? How could you make the force added a function of the change in distance between the vehicle and ground, up to a limited maximum value?
SO as it gets closer to its target distance, the amount of force applied is less so it doesn't go over its target distance.
(current script)
#pragma strict
function Start () {
}
var hoverHeight : float = 10;
function Update ()
{
var ray = this.transform.position;
if (Physics.Raycast(transform.position, -transform.up, hoverHeight))
{
rigidbody.AddForce(0,100,0);
}
}
Answer by meat5000 · Sep 17, 2013 at 11:45 PM
Simply make your AddForce call contain a function of distance and a scaling factor.
Hi, thanks for the reply. How can you get The distance, i changed the script a bit after a lot of googling but i really don't know anything about coding, all i've done was a tiny bit of vb a very long time ago. New script
#pragma strict
function Start () {
}
var hoverHeight : float = 5;
function Update ()
{
var groundLoc = GameObject.Find("ground").transform.position;
var distance = Vector3.Distance(this.transform.position, groundLoc.transform.position);
if(distance < hoverHeight, set distance to = hoverHeight at a certain rate;
{
}
}
the problems i need to fix with this are,
on line 11 I get "bce0019: 'transform' is not a member of 'UnityEngine.vector3'.
I'm not sure that will give me the distance i want, as i only want distance in the global y axis.
How is it possible to do the following: if(distance < hoverHeight, set distance to = hoverHeight at a certain rate;