Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

Advanced search

1401612 Posts in 67930 Topics- by 61478 Members - Latest Member: clubcana

July 02, 2022, 02:25:00 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityDevLogsTHE MILKMAN - Milk Delivery Game
Pages: 1 [2]
Author Topic: THE MILKMAN - Milk Delivery Game  (Read 1414 times)
Level 10

View Profile
« Reply #20 on: June 20, 2022, 02:10:50 AM »

Weird NPC Vehicles

Been away for a while as my wife and I just had a baby  Smiley
This time I'm adding in some vehicles on the road to act as obstacles to Mr. Milkman - but I want to make them move on their own, so scripted a very basic AI system to keep them on the road and not move too weirdly.

Testing out the new NPC vehicle
The first thing I do is import a slightly modified milk truck as an asset into Unity and add a bunch of colliders. I call this the Civilian Truck. The civilian truck still looked pretty unnatural on collision so I changed the mass. I drove around a bit with these static objects to get a feel for how difficult / easy it would be to negotiate turns and slip through the traffic.

I then started work on scripting the movement of these zombie trucks as they weren't much good just sitting there. By playing with the transform figures in unity I figured out that I shouldn't have been scripting a change to the y axis. It should have been to the z axis.

Going forwards and backwards
I then wrote some code to accommodate cars going forwards and backwards. This was simply done by applying different scripts depending on whether a vehicle is facing one way or another, and applying a negative value to the forwardforce for the opposite direction. The major problem with this concept was that I was using 0 degrees or 180 degrees as the trigger for the rest of the movement code. But what if the vehicle bumps into something slightly and changes its direction from 0 degrees to 0.5 degrees? Well that would mean that, that particular vehicle stops, and that's what happened here. You can see the vehicle in front hits the road bump and its rotation in all likelihood changes a tiny bit, meaning it is either not 0 or not 180 degrees exactly, hence it stops. The same applies to the vehicles piling up behind it.

So I thought about a way to do this, where I could list a range of values under which the vehicle would have to turn back to its original direction on the road. I realised that I was out of my depth and probably should have finished that C# class on arrays! So after messing about for a while and getting tangled in my own logic statements, I introduced a boolean value called facingForward, and basically divided the treatment of the code into two categories, those vehicles facing forward and those facing back. It seemed to work but some vehicles that turned past the midway mark appeared to then go the other way on the road, which was odd.

I then messed around a bit more and found the civilian trucks facing me were constantly turning. No matter how I tweaked the script I couldn't seem to fix this issue, until I became very specific about the and/or statements and which values they applied to.

NPCs flipping over
I also wanted to make it so that once the vehicles flipped over or reached a certain rotation, basically they would stop moving forward. So I scripted a long line of conditional statements right at the top to test whether the vehicle at the beginning of that frame was in a weird rotation or not. If it was not in a weird position, then the rest of the code would apply, including moving forward and turning back in the right direction. This worked! I was pretty happy with the result.

Except on further testing I noticed something strange. Some vehicles who had not rotated on the x axis in such a way as to stop its movement were still moving on the road. So I tweaked it further since I wasn't thinking in 3 dimensional space properly. This time I tested out the effect of changing various rotation values to make it easier to clarify which values ought to be changed. And well, it worked! The vehicles were piling up like I expected they would.

Next steps
The next thing I'll work on is building different types of vehicles using the same code so that it doesn't look so boring! I will also work on keeping the traffic flowing so that the cars don't simply run out and deplete, and so that they don't start climbing up the mountain.

Here's my video DevLog with a bit more detail (and comedy moments):

Congratulations to you and your wife!

Love your presentation style where the jokes are delivered so matter-of-factly that you almost miss the part that they're jokes, hahaha.
Level 10

...Not again.

View Profile
« Reply #21 on: June 20, 2022, 07:18:08 AM »

Fun update as always. Glad to see that I'm not the only one who has to iterate through every aspect of what I do 1,000x until it semi-works. The new NPC vehicles add a great element to the gameplay.

Level 0

View Profile
« Reply #22 on: June 22, 2022, 09:49:42 PM »

Thanks @JobLeonard and @oldblood!

It's been a good learning experience - comedy results at times. It's interesting being a solo dev without anyone to immediately bounce ideas off because I think a lot of the mistakes I'm making, especially with C# and the Unity interface, could be easily avoided if I was in a team that had more experience of these things.
Pages: 1 [2]
Jump to:  

Theme orange-lt created by panic