- Home /
Question by
The-Game-Master · Jul 01, 2015 at 10:53 PM ·
scriptingproblemhoverhoveringhovercraft
Need help with my hovercraft vehicle
I have this script: using UnityEngine; using System.Collections;
public class HoverMotor : MonoBehaviour {
public float speed = 90f;
public float turnSpeed = 5f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
private float powerInput;
private float turnInput;
private Rigidbody carRigidbody;
void Awake ()
{
carRigidbody = GetComponent <Rigidbody>();
}
void Update ()
{
powerInput = Input.GetAxis ("Vertical");
turnInput = Input.GetAxis ("Horizontal");
}
void FixedUpdate()
{
Ray ray = new Ray (transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, hoverHeight))
{
float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverForce;
if(!Input.GetKey(KeyCode.Space)){
carRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
}
}
carRigidbody.AddRelativeForce(0f, 0f, powerInput * speed);
carRigidbody.AddRelativeTorque(0f, turnInput * turnSpeed, 0f);
}
}
This is a hover motor. This raycasts down from the current position and determines how much force it needs to add to sustain it's current Y position. However, it will still smack into the ground and start spinning out of control. How can I fix it so that it won't smack into the ground? Thanks! (Yes, this did come from the unity hovercar physics tutorial, I just edited it a bit to fit my needs.)
Comment
Your answer
Follow this Question
Related Questions
How would you make objects appear when the mouse goes over them? 1 Answer
Hovercar Script, object slowly descends instead 0 Answers
How to achieve a stable hover 1 Answer
4.6 UI Hover & Click sound? 1 Answer
Hovering smoothly at one height 1 Answer