- Home /
Hovercraft Physics Problems
I've written my own hovercraft physics, however I can't seem to get it to work. I have Ray Position game objects that I place on different corners of the vehicle to dictate where the raycasts will go. These positions are then used to create the raycasts (going down), then determine where to add force on each corner of the object. I know I have nothing rotating or moving it forward, but what am I missing to get it off of the ground?
EDIT: Okay I figured out how to get it off of the ground with help from Wolfram, now it rocks and flips over a lot how can I fix that?
#pragma strict
var vehicleSpeed = 20;
var rotationSpeed = 10.0;
var airborneForce = 5.0;
var offgroundHeight = 10.0;
var startRotation : Quaternion;
var startPosition : Vector3;
var rayPos : GameObject[];
function Start () {
rayPos = GameObject.FindGameObjectsWithTag("RayPos");
startRotation = transform.rotation;
startPosition = transform.position;
}
function Update () {
var down = transform.TransformDirection (-Vector3.up);
var forwardForce : float = Input.GetAxis ("Vertical") * vehicleSpeed;
var flight = transform.TransformDirection (Vector3.up * airborneForce);
rigidbody.AddForce (transform.forward * forwardForce);
if(Input.GetAxis ("Horizontal")){
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
transform.Rotate (0, rotation, 0);
}
if(Input.GetButtonDown("Jump")){
rigidbody.drag = 50;
}else
if(Input.GetKeyDown("left ctrl")){
transform.rotation = startRotation;
transform.position.y += 2;
rigidbody.drag = 50;
}else
if(Input.GetKeyDown("r")){
transform.rotation = startRotation;
transform.position = startPosition;
transform.position.y += 2;
rigidbody.drag = 50;
}else
rigidbody.drag = 0;
if (Physics.Raycast (rayPos[0].transform.position, down, offgroundHeight)) {
rigidbody.AddForceAtPosition(flight, rayPos[0].transform.position);
}
if (Physics.Raycast (rayPos[1].transform.position, down, offgroundHeight)) {
rigidbody.AddForceAtPosition(flight, rayPos[1].transform.position);
}
if (Physics.Raycast (rayPos[2].transform.position, down, offgroundHeight)) {
rigidbody.AddForceAtPosition(flight, rayPos[2].transform.position);
}
if (Physics.Raycast (rayPos[3].transform.position, down, offgroundHeight)) {
rigidbody.AddForceAtPosition(flight, rayPos[3].transform.position);
}
}
Answer by Wolfram · Jun 27, 2012 at 10:53 PM
a) you are applying a downwards force to the rigidbody, but you need to apply an upwards force. Don't try to apply the "exhaust" from your hovercraft fans, apply the direction in which they would push your hovercraft: up.
b) note arrays start their count at 0, so if you have 4 objects tagged "RayPos", you'll need to access them using indices [0]..[3], not [1]..[4].
c) don't use any Find*-functions in Update, they are extremely slow. Assuming the amount/identity of objects tagged "RayPos" doen't change, do the FindGameObjectsWithTag() only once (i.e., in Start()). If the objects somehow do change, it might be a good idea to put the FindGameObjectsWithTag() in a separate function which is only called when they change.
EDIT: the script evolved while trying to solve all related problems, so the answer itself is rather outdated, but still contains hints to keep in mind. the rest of the answer is distributed in the comments ;-)
Uh, wait, you are using "pos.rayPos[x]", which doesn't make any sense. pos is a Vector3, rayPos[x] is a GameObject. Does this even compile? Please add "#pragma strict" at the top of your script, to prevent mistakes like this.
Replace all occurences with this ins$$anonymous$$d: rayPos[x].transform.position
Could you please update your question with your current script? Also, what exactly isn't working? Forward and rotation work? Doesn't it hover at all, or does it move/jump/jerk around?
Your answer
Follow this Question
Related Questions
What is the best way to apply force, then come to a smooth stop on control release? 1 Answer
Calculating required force for pushing a body to a desired position at once 0 Answers
increasing knockback of a rigidbody as received damage increases (like super smash) 1 Answer