moveSpeed variable question

Instead of change the equation within the move method, would there be any issues that arise by moving the variable initalization to the Start function by calling the waveConfig.GetMoveSpeed? my solution below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPathing : MonoBehaviour
{
[SerializeField] List waypoints;
[SerializeField] WaveConfig waveConfig;
int waypointIndex = 0;
float moveSpeed;

// Start is called before the first frame update
void Start()
{
    waypoints = waveConfig.GetWaypoints();
    transform.position = waypoints[waypointIndex].transform.position;
    moveSpeed = waveConfig.GetMoveSpeed();
}

// Update is called once per frame
void Update()
{
    Move();
}

private void Move()
{
    if (waypointIndex <= waypoints.Count - 1)
    {
        var targetPos = waypoints[waypointIndex].transform.position;
        var movementThisFrame = moveSpeed * Time.deltaTime;
        transform.position = Vector2.MoveTowards(transform.position, targetPos, movementThisFrame);

        if (transform.position == targetPos)
        {
            waypointIndex++;
        }

    }
    else
    {
        Destroy(gameObject);
    }
}

}

Hi,

In many cases, there are multiple ways to make something work in Unity, and Rick cannot show all of them. If your code works, it’s a solution by definition. :slight_smile:

Has your question been answered?


See also:

It makes sense that there are multiple ways accomplishing the same thing, I was just trying to understand if Rick was doing it that way for a reason, or if the way I had figured out would cause issues when trying to later code something else. Thank you.

Game developers are not clairvoyants. If the solution works now, we usually keep it and don’t worry about it until we encounter an issue which is caused by the solution. And if we encounter a problem, it is very likely that we’ll be able to solve it because we wrote the code and (hopefully) know what we did.

There is always a reason. Rick had an idea, and he made it work. If you asked Ben how to solve this problem, it might be that he comes up with a different solution because he’s Ben, not Rick. If you came up with a different solution, that’s fine as well.

It would be different if you discovered a flaw in Rick’s solution and fixed it in yours. In that case, one could discuss the differences in regards to the (perceived) flaw.

I know this might sound a bit strange but this is how “programming” works. There is no universal solution which solves all problems, and we hope that our solutions won’t cause any issues in the future. For this reason, if you solved a problem and don’t see any conflicts with future problems, be like all the other programmers: Keep your solution and move on to the next problem. :slight_smile:

1 Like

That actually makes a lot of sense. Thanks

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms