Hello everyone,
I’m currently playing with unity physics and i’m requesting your help because i’m trying to make a floating vehicule with thrusters and i’m pretty sure that my approach is so naive that I must lack the knowledge and understanding of the principles of basic physics.
So here’s my approach :
- I have my main vehicule as a gameobject with a Rigidbody and a collider
- I have four children that act as thrusters positions : I use their relative position to apply a force on a specific point of my vehicule
- Each tick, I loop inside my thrusters gameobjects and fire a Ray : if they are below the max height I can apply a force
- Based on the distance between each thruster and their hit.point, I multiply the force of my thruster by the normalized distance value. For instance, if the max distance is 10 and the distance between the hit.point and the thruster is 5, I multiply my thruster strength by that normalized value (which in this case is 0.5). My thruster is at 50% of it’s maximum strength.
Here is the complete code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Hoovering : MonoBehaviour
{
const float _GRAV = 9.81f;
//Children object that are placed at thrusters location
public GameObject[] m_hooverPoints;
//Components
private Rigidbody rb;
//Metrics
[SerializeField] private float m_hooverMaxHeight, m_hooverMinHeight;
[SerializeField] private float m_hooverThrust;
[SerializeField] private float m_hooverDamp;
//Testing purposes
[SerializeField] private Color col_minThruster, col_maxThruster;
private void Start()
{
rb = gameObject.GetComponent<Rigidbody>();
if (m_hooverPoints == null)
{
Debug.LogError("No hoover points detected");
return;
}
}
private void Update()
{
TestInputs();
}
private void FixedUpdate()
{
//Check if the model is facing uspide down
if (Vector3.Dot(transform.up, Vector3.down) < 0 )
{
for (int i = 0; i < m_hooverPoints.Length; i++)
{
//Shoot the raycast at the hooverpoint position
Vector3 anchorPointPos = m_hooverPoints[i].GetComponent<Transform>().position;
Ray downRay = new Ray(anchorPointPos, -Vector3.up);
RaycastHit hit;
if (Physics.Raycast(downRay, out hit, m_hooverMaxHeight))
{
//float m_normalizedOffset = m_hooverHeight - hit.distance / m_hooverHeight - hooverOffset;
float m_normalizedThruster = (m_hooverMaxHeight - hit.distance) / (m_hooverMaxHeight - m_hooverMinHeight);
Debug.Log("Thruster " + i + " = " + m_normalizedThruster * 100 + " % of power");
Debug.DrawLine(anchorPointPos, hit.point, Color.Lerp(col_minThruster, col_maxThruster, m_normalizedThruster));
float upwardSpeed = m_hooverPoints[i].GetComponent<Rigidbody>().velocity.y;
float m_lift = (m_hooverThrust * m_normalizedThruster);
rb.AddForceAtPosition(m_lift * Vector3.up, anchorPointPos, ForceMode.Force);
}
}
}
}
private void TestInputs()
{
if (Input.GetKey(KeyCode.UpArrow))
{
rb.AddForce(-Vector3.forward * 100f, ForceMode.Force);
}
else if (Input.GetKey(KeyCode.DownArrow))
{
rb.AddForce(Vector3.forward * 100f, ForceMode.Force);
}
}
}
greenvastcornsnake
But, as you can see in this gif, it’s not really properly working. Right now, it can’t stop bouncing. I guess, i could have a “stable” thruster force (which is like 50% of the max strength) but I don’t really know how to calculate the optimum strength for a stable vehicule. I can’t really handle slopes too. I don’t know if lowering the strength based on the distance is a good idea. Again, my guess would be to apply a normalized but relative value of each thruster. Instead of comparing the distance between the ground and the thruster, find the lowest thruster on the vehicule, relative to the highest thruster.
Oh and I was wondering, how can I retranscript the strength of a thruster based on the mass of my rigidbody ? Does “1” of mass equal “1” of strength applied in an AddForce ?
I’ve already searched for solution on the net, but i’m pretty sure that I don’t really understand how it works (maybe it’s a bad idea to proceed like that), so I was wondering if you could help me to find the best approach.
Thank you for your lecture, and have a nice day,
Klondique