Hover car lean limiting turning angle
Hello, what I want to achieve is to have my hover car lean when it turns so it looks natural.
The problem at the moment, the leaning works and the turning works however, only independently of one another. I assume the clamp angle is where I'm going wrong. Not only clamping the lean angle but the turn angle. I assume this because when both scripts are active, I can lean side to side (the angle clamp works) and move forwards/backwards.
This is the leaning script:
public float MaxTiltAngle = 20.0f; // The maximum angle the board can tilt
public float tiltSpeed = 30.0f; // tilting speed in degrees/second
Vector3 curRot;
float maxZ;
float minZ;
void Start()
{
// Get initial rotation
curRot = this.transform.eulerAngles;
// calculate limit angles:
maxZ = curRot.z + MaxTiltAngle;
minZ = curRot.z - MaxTiltAngle;
}
void Update()
{
// "rotate" the angles mathematically:
curRot.z += -Input.GetAxis("Horizontal") * Time.deltaTime * tiltSpeed;
// Restrict rotation along z axes to the limit angles:
curRot.z = Mathf.Clamp(curRot.z, minZ, maxZ);
// Set the object rotation
this.transform.eulerAngles = curRot;
}
This is the "hovering" and moving script:
Rigidbody m_body;
float m_deadZone = 0.1f;
public float m_hoverForce = 9.0f;
public float m_hoverHeight = 2.0f;
public GameObject[] m_hoverPoints;
public float m_forwardAcl = 100.0f;
public float m_backwardAcl = 25.0f;
float m_currThrust = 0.0f;
public float m_turnStrength = 10f;
float m_currTurn = 0.0f;
public GameObject m_leftAirBrake;
public GameObject m_rightAirBrake;
int m_layerMask;
void Start()
{
m_body = GetComponent<Rigidbody>();
m_layerMask = 1 << LayerMask.NameToLayer("Characters");
m_layerMask = ~m_layerMask;
}
void OnDrawGizmos()
{
// Hover Force
RaycastHit hit;
for (int i = 0; i < m_hoverPoints.Length; i++)
{
var hoverPoint = m_hoverPoints[i];
if (Physics.Raycast(hoverPoint.transform.position,
-Vector3.up, out hit,
m_hoverHeight,
m_layerMask))
{
Gizmos.color = Color.blue;
Gizmos.DrawLine(hoverPoint.transform.position, hit.point);
Gizmos.DrawSphere(hit.point, 0.5f);
}
else
{
Gizmos.color = Color.red;
Gizmos.DrawLine(hoverPoint.transform.position,
hoverPoint.transform.position - Vector3.up * m_hoverHeight);
}
}
}
void Update()
{
// Main Thrust
m_currThrust = 0.0f;
float aclAxis = Input.GetAxis("Vertical");
if (aclAxis > m_deadZone)
m_currThrust = aclAxis * m_forwardAcl;
else if (aclAxis < -m_deadZone)
m_currThrust = aclAxis * m_backwardAcl;
// Turning
m_currTurn = 0.0f;
float turnAxis = Input.GetAxis("Horizontal");
if (Mathf.Abs(turnAxis) > m_deadZone)
m_currTurn = turnAxis;
}
void FixedUpdate()
{
// Hover Force
RaycastHit hit;
for (int i = 0; i < m_hoverPoints.Length; i++)
{
var hoverPoint = m_hoverPoints[i];
if (Physics.Raycast(hoverPoint.transform.position,
-Vector3.up, out hit,
m_hoverHeight,
m_layerMask))
m_body.AddForceAtPosition(Vector3.up
* m_hoverForce
* (1.0f - (hit.distance / m_hoverHeight)),
hoverPoint.transform.position);
else
{
if (transform.position.y > hoverPoint.transform.position.y)
m_body.AddForceAtPosition(
hoverPoint.transform.up * m_hoverForce,
hoverPoint.transform.position);
else
m_body.AddForceAtPosition(
hoverPoint.transform.up * -m_hoverForce,
hoverPoint.transform.position);
}
}
// Forward
if (Mathf.Abs(m_currThrust) > 0)
m_body.AddForce(transform.forward * m_currThrust);
// Turn
if (m_currTurn > 0)
{
m_body.AddRelativeTorque(Vector3.up * m_currTurn * m_turnStrength);
}
else if (m_currTurn < 0)
{
m_body.AddRelativeTorque(Vector3.up * m_currTurn * m_turnStrength);
}
}
Help or guidance would be greatly appreciated.
Answer by tsdavs · Sep 09, 2016 at 01:31 AM
Sorry no the project came to a stop a while ago but I might be picking it up again soon. If you come to an answer please let me know though.
This version of your script works. I did not use it, cause it does some weird movements to the rigidbody. It does need further tweaks, but it's a start.
using UnityEngine;
using System.Collections;
public class Hover_lean : $$anonymous$$onoBehaviour {
public float $$anonymous$$axTiltAngle = 20.0f; // The maximum angle the board can tilt
public float tiltSpeed = 30.0f; // tilting speed in degrees/second
Vector3 curRot;
float maxX;
float $$anonymous$$X;
//void Start()
void Update()
{
{
// Get initial rotation
curRot = this.transform.eulerAngles;
// calculate limit angles:
maxX = curRot.x + $$anonymous$$axTiltAngle;
$$anonymous$$X = curRot.x - $$anonymous$$axTiltAngle;
}
}
void FixedUpdate()
{
// "rotate" the angles mathematically:
curRot.x += -Input.GetAxis("Tilt") * Time.deltaTime * tiltSpeed;
// Restrict rotation along x axes to the limit angles:
curRot.x = $$anonymous$$athf.Clamp(curRot.x, $$anonymous$$X, maxX);
// Set the object rotation
this.transform.eulerAngles = curRot;
}
}
Answer by Sneer1 · Sep 08, 2016 at 10:17 PM
I'm at the same point. Did you found a solution already ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612054746im_/https://answers.unity.com/themes/thub/images/avi.jpg)