Overthinking the small stuff

Spoilers for moving your enemy.
.
.
.
.
Does anyone else think way too much when doing simple things in code? My solution to moving my enemy to a tile:

        int x = Mathf.RoundToInt(waypoint.transform.position.x - transform.position.x);
        int z = Mathf.RoundToInt(waypoint.transform.position.z - transform.position.z);

        Vector3 newPosition = new Vector3((float)x, 0, (float)z);

        transform.position += newPosition;

Gotta calculate the difference between the two entities, then create a new vector so that I can adjust current position accordingly. Then actually move. While the actual answer is:

transform.position = waypoint.transform.position; // Hey just stand where that thing is.

:man_facepalming:

1 Like

One persons “think way too much” is anothers “doing it right”. Other times, one persons “think way too much” is anothers “Why are you taking that shortcut”.

It all depends.

2 Likes

I know its been a few months since this was posted but I’ll put my two cents in here.

I’m actually in the same boat right now I feel. I’ve been studying these courses and, coding in general, for a few months and I feel like this answer will help you: Everyone has their own style.
What I mean is that the instructors in this course all teach in slightly different ways and due to this it can be difficult to apply basic principles to your code. Sometimes an instructor will declare and initialize a variable at the top of the class, but another instructor likes to declare and then initialize in Start( ) or in a later method, both do essentially the same thing, but now your mind has two, three, four different ways of doing the same thing and you start overanalyzing/thinking. Most of the time, we as students know what the instructors are doing because coding is easy at its core and they break it down really well, but when it comes time to do my own code my brain usually starts off like this:
“ok, moving object from Point A to Point B. Ok…do I need the transform? I do, so let me make a variable of that. Oh wait, I don’t need a variable because this transform is on the object… So do i need a vector3 variable? Wait is it being moved each frame? If I put a vector3 do I have to make a ‘new’ vector3 or do I just declare it and then use it somewhere…”
After about 5 minutes of that it would be enough to make anyone go crazy.
But back on topic, the only thing I’ve found that has helped me make my own stuff work without needing to watch tutorials is to break what i’m trying to do down into its simplest parts and then approach each thing one at a time. I’m not big on math or anything but I’m pretty sure the process is called creating an algorithm. So using the example above, I do this approach now:

  1. Create local variable Vector3, use debug log to make sure its actually getting something.
  2. If debug.log is good, how do you make things move? Update method, so add some code to move the Vector3 in a direction, doesn’t matter how far.
  3. If its moving, place an ending logic to the movement code so it stops at that point. Use the transform of the ending point.
    If any of the code fails above, I take it back to step 1 and start again.

Hope this super long rant was helpful, it helped me out :slight_smile:

1 Like

I’d like to second what others have said above. Coding is like taking a trip somewhere. If you’ve never been there, you can follow the guide and get there just fine. However, after you’re more familiar with the territory, you might find other ways of getting from point A to point B. There can be different measures for the “best” way to make the trip. Maybe one is faster but the other is more scenic. It comes down to how you’re measuring what is “best”.

To translate that thought a bit into coding, there are different ways to do the same thing. There may be a best way to do something in terms of how efficient it is, but maybe that way isn’t as easy to understand for others just approaching the code or who have no experience coding. Or, maybe the instructor is trying to do more than just show their students the easiest/most efficent way to do things but instead wants to expose them to additional tools or ways of thinking.

Just my 2¢. Thanks for posting the question @Dreams :slight_smile:

Privacy & Terms