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*;*
  •  	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.

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.

2 Answers

2

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. :slight_smile:

But the performance tricks should help in general :slight_smile:

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

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.