- Home /
two Raycast`s causes lag
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[i];
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[i];
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 ins$$anonymous$$d 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.
I 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.
Answer by ThePersister · Sep 09, 2014 at 08:12 AM
Hmmm, tough one. If you'd like to track down your perfomance issues, I'd suggest using the Unity Profiler, look up some tutorials on Youtube, etc.
Once you get the hang of the profiler, you can track down all the way to your methods where your performance is being used the most and how much % of the total.
Right now, it's pretty hard to see / understand how the script applies in the game. I mean, I can see a hovervehicle flying about, and the player being able to trust it about, but pictures of your scene would help.
Performance tricks:
Each .transform is actually a "GetComponent()", so consider storing it in a variable at start once, and use that variable in the rest of the script. (Saves 11+ GetComponents per frame)
Code for that:
Transform mTransform, mHoverPointTransform;
void Start()
{
// Use further down below :)
mTransform = transform;
mHoverPointTransform = hoverPoint.transform;
carBody = GetComponent<Rigidbody>();
layerMask = 1 << LayerMask.NameToLayer("Characters");
layerMask = ~layerMask;
}
Furthermore, you're using OnDrawGizmos, these will only aid you in the scene view, and I'm not sure how heavy they are on your system, but for as far as game performance, you can ignore it. So, testing your actual performance, perhaps you should close the scene view, or point the camera to an inactive area, so Unity doesn't have to process too much there. If this does cause issues, you'll see this in the profiler as "Overhead", that's when you know the scene view or Debug.Logs are fcking you over :3
Aside from the performance tricks, it seems weird having to apply 2 raycasts to stop you from going up, you should add your own friction based on your velocity.
Add more scene pictures, and I might be able to help with the whole, don't fly away hovercraft please issue. :)
But the performance tricks should help in general :)
Answer by ThierryH · Sep 09, 2014 at 01:06 PM
Thanks for the answer. I attached two images:
1(idle) - when the game is running, but I`m nothind doing 2(moving) - when the hover is moving
Hey there, thx for the images, it's a bit hard to help out not having access to the project itself, but I won't ask for that, so don't worry.
I find it strange that your performance is better whenever you're moving. You have a lot of GPU usage when it comes down to Shadows, unfortunately I'm pretty unadept at shadows, so I won't be able to help much on that matter by personal experience. But you should check that up, because being at 60 FPS is the good life.
As far as CPU usage, it all comes down to the "Others" spikes. I'd suggest commenting out the Gizmos as I stated before, and closing the scene tab, so we don't have to worry about that overhead.
Then you should click and drag with your mouse in the CPU area, and put this vertical indication bar at one of the Brown Spikes, that's when you want to look at the Overview below your recorded data, where you will see the performance take-up at That time :)
Also, I now get why you need the Raycast, as you hover over the ground you want to always be a certain distance away from it, it makes sense now, my apologies.
Yea, I don't think there is any magic solution, just look through the profiler whilst you're at the Time of the Spikes and try to optimize the code / scenepiece that causes it.
If all this is worthy as an answer, please accept it :) Best of Luck! ;3
Thank you. I will accept your answer anyway. But just two more question)) 1) How to limit the hover max height above the terrain, in other way(not what I have now)? 2) And how to strafe (not turn) the hover, as in video below: http://www.youtube.com/watch?v=JE0ZLq5bobc I tried to add transform.rotation = Quaternion.Euler(0,0,25);
but it work uncorrectly, and the code: carBody.AddTorque(Vector3.right * 20); or carBody.AddTorque(0,0,30);
both didn`t work in general.
Your answer
![](https://koobas.hobune.stream/wayback/20220613162549im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to steer a hovercraft that aligns itself to a surface normal? 0 Answers
Using CrossHair GUI to Determine Ray Spread 0 Answers
Raycast From Camera to Object 1 Answer
Audio play problem on raycast hit colider.gameObject.tag 1 Answer
Pushing away rigidbody from the direction of raycast hit 1 Answer