Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411507 Posts in 69374 Topics- by 58429 Members - Latest Member: Alternalo

April 25, 2024, 11:08:02 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityDevLogsTHE MILKMAN - Milk Delivery Game
Pages: 1 [2]
Print
Author Topic: THE MILKMAN - Milk Delivery Game  (Read 5651 times)
JobLeonard
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.
Logged
oldblood
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.
Logged

JeremyNunns
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.
Logged
JeremyNunns
Level 0
**


View Profile
« Reply #23 on: July 07, 2022, 11:03:22 PM »

Hello everyone, an update! Gentleman

New Vehicles
I'm recycling the existing truck and just give it a red colour, and have made a blue version as well. But I realise I need more variety, so head back to Blender to create a typical family car, and add it in in black and yellow.

Spawning
I started work on a teleport system which would teleport a car from the end of the road to the beginning of the road and this was just a matter of creating four entrance portals which would receive the vehicles and 4 exit portals where the cars would pop out of. The scripting was pretty simple and I was happy with the result.

However, I didn't like the fact that you could clearly see the vehicles appearing and disappearing in the distance and also behind the milk Truck if you reversed. So I fixed this by creating an invisible barrier behind the milk Truck so that the player is unable to see the vehicles appearing and disappearing behind you. Likewise I switched back to blender to create a concrete tunnel structure to be placed at the end of the road and which would receive oncoming traffic and spit out vehicles.

I also found a script to 'hack' the light object and create some sort of "negative light" to suck out the light within the tunnel (because the natural shadow wasn't dark enough).

Simplifying code (or how to complicate things further)
I then remembered that there was a comment suggesting that I use the function transform.Forward. I understood it to mean that I could save lots of lines of code. Long story short, it didn't really help and if you want to see the comedy results in trying to make it work, go ahead and check out my latest video DevLog here:






Logged
oldblood
Level 10
*****

...Not again.


View Profile
« Reply #24 on: July 10, 2022, 06:03:39 AM »

Entertaining video as always. I very much relate to going through a lot of work to implement some new feature to improve everything and then scratching it when you realize its created 10 other problems and the original solution (as inelegant as they can be) tends to be the better. My challenge is knowing when to power through all the bugs with a new feature vs. when to know that its better to revert.
Logged

JeremyNunns
Level 0
**


View Profile
« Reply #25 on: July 13, 2022, 08:05:08 PM »

Thanks @Oldblood - I know everyone will have different views on that. I usually stick to a problem if I really feel like it should be solved for learning purposes - but in this case it was just causing too many issues and I want to be able to learn more about different elements of Unity and C# for different types of games I wish to make in the future. So I guess it's a mixture of judging how valuable the new feature is compared against your ultimate goal (at least for me).  Gentleman
Logged
JeremyNunns
Level 0
**


View Profile
« Reply #26 on: August 29, 2022, 06:44:50 AM »

WHEW being a dad is tiring folks!  Gentleman

Been a while since my last update and I've worked on a number of things, which you can see in my DevLog video below. I asked my wife to have a go at my game and... well, you'll see what her thoughts were!

I just wanted to highlight something which I really didn't think I'd get stuck on - and that was implementing some OOP with C# / Unity and referring to variables between different scripts. For some reason I'm just not doing it right! I ended up just creating these fake empty game object counters to act as information go-betweens for scripts - which I am pretty sure is super inefficient and plain wrong.

Link to latest DevLog as usual:


Logged
Ishi
Pixelhead
Level 10
******


coffee&coding


View Profile WWW
« Reply #27 on: August 30, 2022, 02:49:00 PM »

Your devlog is very detailed! Wishing you all the best with finishing this game up - it looks like a great learning exercise. Coffee

Simplifying code (or how to complicate things further)
I then remembered that there was a comment suggesting that I use the function transform.Forward. I understood it to mean that I could save lots of lines of code. Long story short, it didn't really help and if you want to see the comedy results in trying to make it work, go ahead and check out my latest video DevLog here:

I know nothing about Unity specifically but here's my interpretation of what the transform.forward code is doing, assuming Unity behaves how I would expect it to Grin . I didn't see any explanation like this in the youtube comments so it might be helpful to learn what went wrong.

rb.velocity = civTruck.transform.forward * forwardSpeed

"rb.velocity =" is setting the velocity of the rigid body. This is overriding any velocity that the physics system has previously calculated which is why the cars no longer seem to care if they collide with the player's vehicle and keep pushing forwards, and are no longer affected by gravity, etc.

transform.forward gives you the current forward vector of the car. So the result of this code is simply to make the car move along that forward vector at the "forwardSpeed" speed. If the car is perfectly flat on the road and pointing in the right direction then that's fine. But as soon as the car starts to point in a different direction, it'll start to move in that direction instead. Cars that point slightly into the air will start to fly and cars that point down to the ground will keep pushing down into the ground.

Rigid bodies also have a rotational velocity (how fast the angle is changing) which isn't being overridden like the velocity is. So if the car is hit by something it may start to spin according to physics. This explains the behaviour where the cars are floating through the air but still freely spinning.

If you do still want to improve this area of the code: I would suggest splitting the problem into first deciding where the car wants to be (a position in the correct lane of the road but further along it?), then some combination of turning or moving towards that target position. The Unity rigid body should have some kind of function like "ApplyForce" or "AddForce" which would be the correct way to influence the car's movement without overriding the velocity completely.

Alternatively there may well be some useful feature already built into Unity that would allow the car to navigate itself in a smarter way? (Or, as a last resort you could just have any cars that stray off the road explode, then they're no longer a problem Evil )
Logged

JeremyNunns
Level 0
**


View Profile
« Reply #28 on: August 30, 2022, 11:02:12 PM »

Thank you Ishi!  Gentleman

That's very helpful and explains both the strange movement into the sky and also 'into' the ground.

Indeed this has been an amazing learning experience and perhaps I shouldn't have started with 3D - but I just couldn't resist!

I actually really like the idea of just letting cars combust  Cheesy Maybe I'll add a bonus level with this.
Logged
JeremyNunns
Level 0
**


View Profile
« Reply #29 on: October 09, 2022, 01:06:54 AM »

Sound & Music

Finally - to a part of GameDev I'd been waiting to explore for the first time. I'm personally a massive fan of high quality sounds and music, so introducing them to my (admittedly silly) game was a lot of fun.  Gentleman

Car Collision Sounds
I introduced a bunch of car crash sounds to the player vehicle when it collides with the world, then added randomising code for pitch and selection of audioclip so that it didn't sound like it was the same sound being played again and again. Honestly, couldn't have been happier with this part - although I did at one point consider tying the velocity or force of the collision to the volume of the collision sound being played.

I introduced the same set of code to all the other NPC vehicles, but it sounded horrible! Just a cacophany of car crunching because the NPCs were constantly crashing. I didn't really want to waste my time trying to dumb it down or introduce distance from the audio source.

Milk Bottle Smash Sounds
Pretty similar to the car crash sound - the bottle smashes sound crisp and satisfying - but I did struggle with the code initially because I was attaching it to an object which was being destroyed (and hence the audiosource was being destroyed as well).

Car Engine Hum
This was one of my favourite parts - the idle hum of the engine, adjusted for speed and acceleration. Humblebrag - I had the code all planned out in my mind and fully expected it not to work (despite its relative simplicity), but once I tweaked a couple of things, it did actually work quite well!

Music!
My friend, Adam Grindley composed a couple of background songs for my game - frankly I think his work outshines my entire project as the quality is very high and he matched my brief very well (which was basically along the lines of "chaotic, but fun and silly... a little like the Overcooked games").

If you want to have a listen to the sounds and music I've now added, here's my latest Devlog update:


Logged
oldblood
Level 10
*****

...Not again.


View Profile
« Reply #30 on: October 14, 2022, 05:24:25 AM »

Great devlog as always. Looks (and sounds) good. I like how you include troubling through the bugs and what resolution you ended up implementing. Makes each video feel like a journey.
Logged

JeremyNunns
Level 0
**


View Profile
« Reply #31 on: October 17, 2022, 06:30:46 AM »

Thanks oldblood! Gentleman

Really appreciate the comments. It's mostly just a whizz through all the challenges I face during the development. Making videos is hard work but hugely rewarding when you see people enjoy it, and it's great for keeping me interested in the development of my game(s) as it naturally creates a community.

Logged
JeremyNunns
Level 0
**


View Profile
« Reply #32 on: November 30, 2022, 02:54:08 AM »

Adding Levels

I've added two levels.

Level 2: A night time level

I basically switched off the skybox for this one and could see that my streetlamps weren't working properly. For some reason the light was shining on the grass terrain but not the road object.

After some digging around, I figured out that Unity has a limit on the number of light sources a single object can reflect, and this was one long road object - so it was maxing out at about 4 light reflections! Anyway I solved this by splitting the road object up and it worked.

I also experimented with the headlamps of various vehicles and was surprised with how much it enhanced the game. Although adding headlamps to the civilian vehicles was too much for Unity again and created some very strange effects, so I backtracked on this.

Level 3: Big milk bottle

The final level will involve trying to get a very large milk bottle to the end of the road, instead of 32 smaller bottles. This is much harder as you've got one big humpty dumpty at the back of the truck and you can't let it break!

I'm excited to hopefully release this soon and get some feedback on it.

If you want to see what I meant by the lights and big milk bottle, here's my video devlog:


« Last Edit: December 05, 2022, 04:44:07 AM by JeremyNunns » Logged
oldblood
Level 10
*****

...Not again.


View Profile
« Reply #33 on: December 04, 2022, 10:08:42 AM »

Great vlog as always. Appreciate how you always show the walls that you hit in development than how you eventually solve them. All very relatable content even when your game isn't about delivering milk haha. Looks like you're getting close to the finish line on all this. Whats the release plan for this?
Logged

JeremyNunns
Level 0
**


View Profile
« Reply #34 on: December 06, 2022, 06:17:59 PM »

Thanks oldblood! Yep hopefully wrapping up with a leaderboard, and then I'll publish - will keep you all posted  Gentleman
Logged
Pages: 1 [2]
Print
Jump to:  

Theme orange-lt created by panic