Hello,
I need your help.
I make a Hover-car movement script.
Distance to ground and the Hover movement is calculated by Raycast,
I make another Raycast, because there was a problem - hover flew too high if you hold the button pressed.
But now with two Raycast it cause a lag - Hover moves in jerks.
Interpolate does not help.
How can I make smooth movement without jerking?
Please, help.
public class hoverController_v7 : MonoBehaviour {
Rigidbody carBody;
float deadZone = 0.1f;
public float hoverForce = 9.0f;
public float hoverHeight = 2.0f;
public GameObject[] hoverPoints;
public float forwardAcceleration = 100.0f;
public float backwardAcceleration = 25.0f;
public float currThrust = 0.0f;
public float turnStrength = 10.0f;
public float currTurn = 0.0f;
public GameObject leftAirBrake;
public GameObject rightAirBrake;
int layerMask;
void Start()
{
carBody = GetComponent<Rigidbody>();
layerMask = 1 << LayerMask.NameToLayer("Characters");
layerMask = ~layerMask;
}
void OnDrawGizmos()
{
// сила hoverForce
RaycastHit hit;
for(int i = 0; i < hoverPoints.Length; i++)
{
var hoverPoint = hoverPoints*;*
-
if(Physics.Raycast(hoverPoint.transform.position,* -
-Vector3.up, out hit,* -
hoverHeight,* -
layerMask))* -
{* -
Gizmos.color = Color.yellow;* -
//Color if correctly alligned* -
Gizmos.DrawLine(hoverPoint.transform.position, hit.point);* -
Gizmos.DrawSphere(hit.point, 0.5f);* -
}* -
else* -
{* -
Gizmos.color = Color.red;* -
//Color if incorrectly alligned*
_ Gizmos.DrawLine (hoverPoint.transform.position, hoverPoint.transform.position - Vector3.up * hoverHeight);_
-
}* -
}* -
}*
-
void Update()*
-
{*
-
// main thrust* -
currThrust = 0.0f;* -
float aclAxis = Input.GetAxis("Vertical");* -
if(aclAxis > deadZone)* -
{*
_ currThrust = aclAxis * forwardAcceleration;_
-
}* -
else if(aclAxis < -deadZone)* -
{*
_ currThrust = aclAxis * backwardAcceleration;_
-
}* -
// Turning* -
currTurn = 0.0f;*
_ float turnAxis = Input.GetAxis(“Mouse X”) * 2;_
-
if(Mathf.Abs(turnAxis) > deadZone)* -
currTurn = turnAxis;* -
}*
-
void FixedUpdate()*
-
{*
-
// hover force* -
RaycastHit hit;* -
for(int i = 0; i < hoverPoints.Length; i++)* -
{*
_ var hoverPoint = hoverPoints*;_
_ if(Physics.Raycast(hoverPoint.transform.position,_
_ -Vector3.up, out hit,_
_ hoverHeight, layerMask))_
_ carBody.AddForceAtPosition(Vector3.up * hoverForce * (1.0f -(hit.distance / hoverHeight)),_
_ hoverPoint.transform.position);_
_ else*_
* {*
* if(transform.position.y > hoverPoint.transform.position.y)*
_ carBody.AddForceAtPosition(hoverPoint.transform.up * hoverForce, hoverPoint.transform.position);
* else*
* //add force to car*
carBody.AddForceAtPosition(hoverPoint.transform.up * -hoverForce, hoverPoint.transform.position);_
* }*
* }*
* // forward*
* RaycastHit hit2;*
* if(Physics.Raycast(transform.position, -transform.up, out hit2))*
* {*
* if(hit2.distance < 2)*
* {*
* if(Mathf.Abs(currThrust) > 0)*
* {*
_ carBody.AddForce(transform.forward * currThrust);_
* }*
* }*
* }*
* // turn*
* if(currTurn > 0)*
* {*
_ carBody.AddRelativeTorque(Vector3.up * currTurn * turnStrength);
* }*
* else if(currTurn < 0)*
* {*
carBody.AddRelativeTorque(Vector3.up * currTurn * turnStrength);_
* }*
* }*
}

Two raycasts instead of one should not be a performance problem. Since your two raycast are almost identical, it is more likely the two bodies of code inside the two raycasts are somehow fighting with each other.
– robertbuI think the lags are not meant in terms of performance (fps) but in terms of the vehicle's movement. That's not entirely clear here though.
– khenkel