- Home /
Rotation+BoxCast works with Update in game editor, but not when built or in FixedUpdate.
Hi! I'm making a mech game where you can enter a secondary seat to see a radar. We want to model the radar a little bit more, so it rotates a box on the mech then casts Physics.BoxCastAll and than creates small red boxes where it collides. The boxes are in a radar layer so only viewable from the navigator camera. This is done in a Update function. The boxes are also created when it hits terrain that sticks up.
It works great when I press play in the editor. But when I build and play there are very large spaces between the boxes ( radarBlips ) that get's created. I tried changing to FixedUpdate and noticed the same behavior. Is the game switching to FixedUpdate when you build? In the editor play I hover around 180-240fps, if that plays a part. HitList is a pool of radarblip boxes that I reuse to improve performance.
public class RadarSweepScript : MonoBehaviour
{
[SerializeField] public Transform RadarBlip;
public RadarHitList<Transform> HitList;
private Transform radarSweepTransform;
private Transform mechTransform;
public float rotationSpeed;
public float radarDistance;
[SerializeField] public LayerMask RadarLayer;
private RadarTargetComputer targetProcessor;
private Collider radarSweepCollider;
private bool radarOn = true;
public bool RadarOn { get => radarOn; set => radarOn = value; }
private float xSweepRotationAngle;
// Start is called before the first frame update
void Awake()
{
radarSweepTransform = transform;
radarSweepCollider = radarSweepTransform.GetComponent<Collider>();
mechTransform = transform.parent.parent;
targetProcessor = gameObject.GetComponent<RadarTargetComputer>();
}
void Start()
{
HitList = new RadarHitList<Transform>(300);
for (int i = 0; i < 300; i++)
{
var radarblip = Instantiate(RadarBlip, transform.position + Vector3.down*5, new Quaternion());
HitList.Add(radarblip);
}
}
void Update()
{
if(!radarOn)
return;
//radarSweepTransform.eulerAngles -= new Vector3(0, rotationSpeed * Time.deltaTime, 0);
xSweepRotationAngle -= Time.deltaTime * rotationSpeed;
radarSweepTransform.rotation = Quaternion.Euler(0, xSweepRotationAngle, 90);
RaycastHit[] hits;
hits = Physics.BoxCastAll(radarSweepCollider.bounds.center, radarSweepTransform.localScale, radarSweepTransform.forward, radarSweepTransform.rotation, 500, RadarLayer);
foreach(RaycastHit hit in hits) {
if(hit.distance > 5) {
var nextHit = HitList.AdvanceNext();
var nextHitScript = nextHit.GetComponent<RadarBlipScript>();
nextHitScript.gameObject.SetActive(true);
nextHitScript.ResetAppearTime();
nextHit.position = hit.point;
nextHit.rotation = Quaternion.identity;
targetProcessor.AddRadarHit(nextHit);
}
}
}
Answer by Shrimpey · Feb 17 at 03:46 PM
Game is not switching to fixedUpdate, but it's probably turning VSync ON by default for the build. This forces the game to run 60 fps (or other FPS equal to your display's refresh rate). There is a setting for turning vsync off. But you shouldn't just leave vsync off and call it a day. You need consistent behaviour that's not dependant on user's framerate and you should program physics related things in FixedUpdate. Do your programming in FixedUpdate, maybe adjust your rotationSpeed to work well in FixedUpdate or some other variables and you should be fine.
Your answer
Follow this Question
Related Questions
How to get Raycast surface normal to align particle system? 1 Answer
Detect if on ground? 1 Answer
RayCast offset rotation 20 degrees 1 Answer
Rigidbody.AddForce with Raycast? 1 Answer
Look At Player with Clamping. 1 Answer