Hi guys, really really great course!
Instead of using your method, of moving the formation on every update, I implemented the moving of the formation in a little bit different way (removed unrelated code for brevity):
void Start () {
float distance = transform.position.z - Camera.main.transform.position.z;
var leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distance));
var rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance));
xmin = leftmost.x + (0.5f * width);
xmax = rightmost.x - (0.5f * width);
GetComponent<Rigidbody2D>().velocity = Vector3.right * speed;
}
void Update () {
if (transform.position.x >= xmax)
{
GetComponent<Rigidbody2D>().velocity = Vector3.left * speed;
}
else if (transform.position.x <= xmin)
{
GetComponent<Rigidbody2D>().velocity = Vector3.right * speed;
}
}
I got a few questions to ask:
- Is there any pros and cons of using either method?
- I don’t really see how can I use the Time.deltaTime. Do I even need to do it, or the velocity is automatically considering the frame rate?
- I notice that when I scale the speed up and down during play mode, the speed doesn’t always change. Sometimes it does. Sometimes it doesn’t. I’m working on windows. Is this a bug?
(4. I notice that I have a small bug in my code. Let’s see who finds it first
)
Thx,