hello,
i am trying to create a prediction system which wont take too much of the CPU,
i have seen and attempted other code, but it raises the cpu time from 5 ms to 600ms… this is not good…
so im trying to fix that with this code…
the issue is that i have a recursive point in the code in which a function starts the sequece of other functions including itself… but when i do so unity stops working… obviously there is something that is missing to make this work…
here is the code:
void Update() //possibly change to Fixedupdate later
{
if(!cueInstantiator.getCueAvailable())
{
startedShowTrajectory = false;
return;
}
unitVector = cueRotationManager.getUnitVector();
cueStrength = cueRotationManager.getCueStrength();
positionToHit = cueRotationManager.getPositionToHit();
if(!cueRotationManager.getCueManagerActive())
{
force = (-1)*unitVector*DEFAULT_FORCE;
}
else
{
force = (-1)*unitVector*cueStrength;
}
if(startedShowTrajectory)
{
return;
}
ShowTrajectory();
startedShowTrajectory = true;
}
void CreateMovementMarkers()
{
if(i % interval == 0)
{
GameObject g = GameObject.Instantiate(marker, ball[0].transform.position, Quaternion.identity);
markers.Add(g);
SceneManager.MoveGameObjectToScene(g, SceneManager.GetSceneByBuildIndex(1));
}
}
void ShowTrajectory()
{
if(!cueInstantiator.getCueAvailable())
{
return;
}
SyncBalls();
}
void SyncBalls()
{
for(int i = 0; i <= 15; i++)
{
ballRB[i].velocity = Vector3.zero;
ballRB[i].angularVelocity = Vector3.zero;
ball[i].transform.position = originalBall[i].transform.position;
ball[i].transform.rotation = originalBall[i].transform.rotation;
}
MoveForwardInTime();
}
void MoveForwardInTime()
{
ballRB[0].AddForceAtPosition(force, positionToHit, ForceMode.Impulse);
for(i = 0; i < stepsToMoveForward; i++)
{
scene.GetPhysicsScene().Simulate(Time.fixedDeltaTime);
CreateMovementMarkers();
}
i = 0;
ClearTrajectory();
//ShowTrajectory();
}
void ClearTrajectory()
{
foreach(var GO in markers)
{
Destroy(GO);
}
markers.Clear();
}
the error starts at the end of the MoveForwardInTime() function where i commented out the issue… it works now becuase it is not recursive now… although when i uncomment it out it will cause unity to stop working…
i would like the commented out function to be part of the code and to be at that position because i want the code to update itself whenever it finishes… which is that point in the code…