Unity Frame Rates and object movement
I'm curious about finding a better processing solution to this. I'm still in my early infancy of programming, and have been using unity for a few days now.
My question:
What is the most efficient way to buffer an object that moves based off a Time.time cycle.
I have an object which travels a fixed distance. It is then shifted on the y-axis by a preset amount.
The problem with this; there is an extremely small amount of time in which the object resets on path1, before being directed onto path2.
I've been able to account for this using co-routines...but when I get to to a tolerance of about 0.01s it's processing dependent and can sometimes fail.
Definitely over-complicating the explanation...so I hope someone smarter than me can point me in the right direction. Thanks in advance.
public class ScanSheet : MonoBehaviour { [SerializeField] Vector3 vectorMagnitude = new Vector3(0, 0, -1); [SerializeField] [Range(0.5f, 20f)] float period = 8.02f;
enum State { Moving, FinishedMoving, GoToLine1, GoToLine2, GoToLine3 };
State state = State.Moving;
bool lineFinished = false;
void Start()
{
}
void Update()
{
//make a method that moves the MusicGuide from left to right
ScanLeftRight();
}
public void ScanLeftRight()
{
// to movement bug at start of each line. Skips to starting position then down to next line.
float timer;
timer = Time.time % period;
Vector3 line1 = new Vector3(6f, 9f, 4f);
Vector3 line2 = new Vector3(6f, 7f, 4f);
Vector3 line3 = new Vector3(6f, 5f, 4f);
Vector3 finalPosition1 = new Vector3(6f, 9f, -4f);
Vector3 finalPosition2 = new Vector3(6f, 7f, -4f);
Vector3 finalPosition3 = new Vector3(6f, 5f, -4f);
if (state == State.Moving || state == State.GoToLine1 && !lineFinished)
{
LineMovement(line1, finalPosition1, timer);
}
else if (state == State.GoToLine2 && lineFinished)
{
LineMovement(line2, finalPosition2, timer);
}
else if (state == State.GoToLine3 && !lineFinished)
{
LineMovement(line3, finalPosition3, timer);
}
}
public void LineMovement(Vector3 a, Vector3 b, float t)
{
transform.position = a + vectorMagnitude * t;
if (transform.position.z <= b.z)
{
StartCoroutine(BufferCoroutine());
}
}
IEnumerator BufferCoroutine()
{
if (state == State.Moving || state == State.GoToLine1)
{
lineFinished = true;
yield return new WaitForSeconds(0.105f);
state = State.GoToLine2;
}
else if (state == State.GoToLine2)
{
yield return new WaitForSeconds(0.105f);
state = State.GoToLine3;
lineFinished = false;
}
else if (state == State.GoToLine3)
{
yield return new WaitForSeconds(0.105f);
state = State.GoToLine1;
}
} }
Your answer
Follow this Question
Related Questions
Frame Base Jump Loop 0 Answers
Custom Rigidbody Controller and Moving Platforms 0 Answers
Coyote Timer Gives Double Jump Frames. 1 Answer
Mirror Networking Moving platform jittering 0 Answers
rigidbody2d on a moving stage 1 Answer