Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411423 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 18, 2024, 02:52:27 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 5625 times)
JeremyNunns
Level 0
**


View Profile
« on: March 21, 2022, 07:08:42 AM »

THE MILKMAN

Introduction:
Hello everyone! I'm Jeremy and I'll be making my first proper (but silly) game The Milkman - a simple timed driving game where you have to deliver your milk bottles without losing too many of them along the way! Don't make too many hard turns and try to avoid the traffic... Above all, do your duty as a Milkman and deliver your milk.

My purpose in making this game is really just to learn more about Unity, C# and Blender.

Devlog:
I hope to post twice a week (dayjob permitting). I've already committed myself to recording my journey on YouTube so if you want to sit back and relax, please do hop over to my YouTube channel: https://www.youtube.com/channel/UCEyjD6YKxZ82ay1LrUl7uPw
« Last Edit: October 09, 2022, 02:53:47 AM by JeremyNunns » Logged
JeremyNunns
Level 0
**


View Profile
« Reply #1 on: March 21, 2022, 07:33:58 AM »

MILKMAN - DEVLOG PART 1 - Game Ideas and Blender

Game Ideas and Visualisation

I drafted some notes covering:

 - What type of game was I looking to create? Well, I had just made the BRackey's 'rolling ball' game tutorial and thought the game itself was a little boring, so I decided to try and improve upon it. I recognise that it's an 'avoid the obstacle' type of game, so I'll stick to that, but make it more entertaining somehow.

 - But how do I make it more entertaining? Make the player carry something that's prone to falling off? Make it timed?

 - Subject matter: Ship container holding cargo? Gas delivery van holding gas cylinders? Milk delivery truck holding milk bottles?

I then drew some pictures, having settled on a milk truck delivering milk bottles (and wish I could upload them - but I don't have my own URL yet!). Key thing will be to make the milk bottles fall off pretty easily, so I need soft suspension on the milk truck.

Finally, I also considered aspects such as music and sounds, which should be a bit cheesy, happy and simple. I thought about the most entertaining gameplay - basically the player is to reach the end of the level with the fastest time without spilling milk. Story is, well, pretty much the same!

Blender and the Milk Truck

This is the first time I'm using Blender, and I have to say it is an incredibly user-friendly piece of software. I do follow a tutorial on how to put a basic truck together, and pretty soon (like after several hours!) I've got a milk truck.

If you want to see the process, I've recorded it all in the following YouTube video:



Logged
JobLeonard
Level 10
*****



View Profile
« Reply #2 on: March 21, 2022, 07:37:39 AM »

Quote
This is the first time I'm using Blender, and I have to say it is an incredibly user-friendly piece of software.
I'll take "things I never expected anyone to say, ever" for 500, Alex

Anyway, enjoy the learning process! I'll probably watch the videos too, they look nicely bite-sized Smiley
Logged
JeremyNunns
Level 0
**


View Profile
« Reply #3 on: March 21, 2022, 07:44:18 AM »

Quote
This is the first time I'm using Blender, and I have to say it is an incredibly user-friendly piece of software.
I'll take "things I never expected anyone to say, ever" for 500, Alex

Anyway, enjoy the learning process! I'll probably watch the videos too, they look nicely bite-sized Smiley

Hahaha fair enough Gentleman I found it fairly intuitive after I started messing around with the different tools to see what worked and what didn't. Still learning of course and the shapes I'm making are akin to pre-kindergarten level probably.

Also, appreciate someone following along! I've got lots to improve in pretty much every direction...
Logged
JeremyNunns
Level 0
**


View Profile
« Reply #4 on: March 24, 2022, 02:23:37 PM »

MILKMAN - DEVLOG PART 2 - Basic Movement

1. Applying Old Script to New Object
Welcome to Part 2 of my game devlog Gentleman Following on from Part 1 of my DevLog where I covered basic game ideas for my milk delivery game, and making a milk truck 3D model in Blender. I have now explored various concepts of the truck movement as I convert the movement functions from the movement of a ball, and applying it to the truck to see if it works.
 
Principally I detach the car from the ball. I wanted to recycle the code I had from the red ball movement instead.

I'm not sure why the code only enabled the car to go from side to side (I think it's because the forward movement was not governed by any keys in the previous Brackeys tutorial game), so I messed around with the forward force by trying to copy the sideways force, and eventually got it to move, but it shot off like a rocket! So I messed around with the drag and mass of the wheels. Unfortunately, no matter how I tried to change the drag and mass of the vehicle, I just could not find a movement that was natural.

2. Parents and Children
I then wrote a very basic script to link the transform positions of the wheels to the chassis of the Truck, with some very weird results! Essentially I was trying to establish at the start of the game what the difference in transform positions of the wheels and the chassis were, and set that as the offset value. Somehow Unity did not like what I was trying to do and I think it had something to do with the colliders pressing against each other. Not sure.

Anyway I eventually figured out how to fix it - simply making the wheels a parent to the chassis of the car. This appeared to work quite well.
At this point I realised an earlier script I wrote to keep the chassis and the wheels together might be redundant, so I turned it off to see if the vehicle stayed together, and it did.

I wanted to group the wheels and the chassis objects together as children of a game object called "truck" and see if I could balance out the mass of the wheels and chassis to fix the weird movement. Then I came across something quite strange where the box collider for the truck reached far lower than the truck and forced the truck to pop up when the game started. Anyway although this was quickly fixed, I did not like the fact that the wheels were left alone, so I decided to put colliders on the chassis and the wheels separately.

3. Colliders, Colliders Everywhere
The collider for the chassis becomes complicated when I need to adapt it to the extruded trailer at the back of the truck. I believe I did it a primitive way because I read somewhere you can use a mesh collider to match the shape of the object - but I didn't research this properly as I couldn't really find anything on Google. I ended up just using multiple box colliders organised into various planes.

This meant that the wheels now sunk into the ground, and where I experimented with different colliders, including the wheel colliders, which totally changed my code for the movement of the truck. I'll go into wheel colliders in my next update! Huge learning curve.

4. Reflections
I'm enjoying this process of learning by trial and error and had no idea I could learn so much from just sticking to a vision and trying my best to execute that vision in a playful, explorative way. This means messing around with scripts and not really knowing what will happen - but that's ok and I reckon it's a great way to learn.

If you're interested in seeing the video devlog version, go ahead and check it out here:



Logged
JeremyNunns
Level 0
**


View Profile
« Reply #5 on: April 03, 2022, 03:36:31 AM »

Milk Truck Wheel Colliders

Welcome to Part 3 of my Milkman Devlog. In my last update, I fixed box colliders to the trailer and scripted some basic movement for the truck, which looked weird.

I had some capsule colliders for the wheels, but it just wasn't working. I decided to look at the other colliders available and 'lo and behold there was an option for "wheel colliders" and well, I've got wheels here so maybe it'll work?

Splitting the wheels up

Firstly, I realised that I was applying one wheel collider to 4 different wheels, which obviously doesn't make sense. You can only have one wheel collider assigned to one object.

So, I tried splitting up the 4 wheels but realised they constituted one object. I couldn't find a way to split them up in Unity, so I went back into Blender. I didn't know how to do this either and eventually just made the wheels again. I also added the word "MILK" onto each wheel so I could see if the wheels were spinning.

Applying wheel colliders

When I started adding wheel colliders in Unity, strangely I couldn't see the shape and position of the collider. This turned out to be because I hadn't added a rigidbody component. So I fixed this and set the wheel colliders to the centre of the wheel. This became an issue which cropped later, as explained below.
I tested how the wheels reacted to the ground, by just lifting up the truck and dropping it. I have to say this was pretty satisfying.

Scripts and feeling guilty...

I pulled an arcade game style movement script online for wheel colliders. Now, I had some initial concerns about just copying code and not feeling like it was my own creation, but decided to push ahead anyway.
The general idea is that under FixedUpdate, if up or down is pressed, a value is changed, and that translates to a motortorque force applied to the front two wheels. Similarly once the spacebar is pressed, there is a breakingforce applied to all four wheels.

The result is that the truck moved like a vehicle!

Now at this point the wheel objects were useless and didn't move. The invisible wheel colliders were doing all the work instead. So I had to script a method which constantly updated the positions and rotations of the wheel objects to the wheel colliders.

But the wheels didn't attach properly - it was at a weird angle. I knew it had something to do with the imported rotation values of the wheel objects from Blender, but I wanted to see if I could fix it via the script by setting an offset quaternion rotation value... and... I failed! So I went back into Blender to reset the object's point of origin and rotation values - but this took a lot of trial and error switching between object mode and edit mode. I ended up having to re-import the wheels many times until I got them right.

But then I got it pretty much right - and for the first time I saw the front wheels turn left and right, and saw them spin around. I felt less guilty about just using somebody else's script - so long as I understood generally what was going on.

It's a feature...

I then decided I wanted the car to slow down when no movement inputs were applied. I basically deleted the redundant brakeforce code and recycled it into an if / else statement to reflect this artificial drag on the vehicle.

But then the car kept flipping over too easily. This would take another few hours to rectify. I tried creating an artificial ballast, which just made the car move really strangely - so I scrapped that. Instead I ended up changing the point of origin again and setting the wheel colliders to the outside of the wheels, to give the vehicle a larger base.

Even to this day the vehicle is unstable, but you know what they say, it's not a bug, it's a feature!

Next Steps

I'm now playing around with adding milk bottles to the trailer getting a feel for the gameplay mechanics. I've also created milk bottles which I will detail soon.

Here's my usual DevLog in video format as well if you prefer:



Logged
JobLeonard
Level 10
*****



View Profile
« Reply #6 on: April 03, 2022, 05:49:10 AM »

You're pretty good at documenting your progress, do you have experience with that kinda thing?
Logged
JeremyNunns
Level 0
**


View Profile
« Reply #7 on: April 03, 2022, 08:39:15 PM »

You're pretty good at documenting your progress, do you have experience with that kinda thing?

Thank you! This is my first DevLog or public blog of sorts. I do enjoy videography / film as well hence the video format for updates, which I feel like can be easier for some to connect with my projects.
« Last Edit: April 05, 2022, 05:27:13 PM by JeremyNunns » Logged
JeremyNunns
Level 0
**


View Profile
« Reply #8 on: April 11, 2022, 09:27:29 AM »

Milkman - Milk Bottles and Gameplay

Update
I made some milk bottles to load onto the truck, and started testing the gameplay.

I was previously messing around with wheel colliders and one essential component of the wheel colliders is the suspension. I had to balance this against the truck flipping over too easily, and it required a lot of initial testing using placeholder milk bottles, which were really just cylinder objects.

Making a Glass Milk Bottle in Blender
These cylinders were actually too big at the end of the day and I decided to create 3D milk bottles in Blender and found a particularly good tutorial online of how to do this. There were some aspects of making the 3D milk bottle that I understand, but the tutorial appeared to use fairly complicated modifiers for certain parts of the bottle which were too advanced for me. In any event I used some of these, and added my own flair to the glass bottle, including a milk-bottle cap. Using reference photos on Google was very important for this. I have to say the milk bottle turned out looking pretty nice and shiny.

I also had in mind that I wanted to make the milk bottle destructible, in that it would smash and pour milk out once it fell out the truck and hit the road. However this was a fairly complicated process which I will make another update about later.
I then imported my first milk bottle into Unity and made it relatively to scale, and fixed the materials applied to the bottle cap and the glass bottle.

Shader Graphs and Liquid Levels
I also wanted to create an effect where the milk bottle would show the level of liquid sloshing around inside the milk bottle. In this respect I made an inside and an outside layer for the glass bottle - basically it was a container within a container. I found tutorials on shader graphs in Unity on giving this liquid fill effect - this was the first time using shader graphs and took a little bit of getting used to.

I then started perfecting the shader on the bottle so that the milk would always fill the container towards gravity - but this was met with varying success. I eventually got it working after playing around with the connectors in the shader, and I thought it looked pretty cool.

So I started loading the truck up and realised I hadn't added rigidbodies to them. After fixing this, they reacted to gravity, but fell right through the truck!

Also strangely one of the milk bottles stayed suspended in mid-air defying gravity with some unknown magic. For some reason I had ticked 'is kinematic' which disapplies forces.

I tried applying mesh colliders to the milk bottles to fix this - but I just couldn't get it to work, so I ended up using box colliders and artificially filling up the shape of the milk bottle.

Playtesting with the Milk Bottles
So again I filled the trailer up with milk bottles and re-tested - and it worked! It was pretty cool to see the milk bottles in action and falling off the trailer. It was also cool to see the detail of the shader working to show the level of milk within the bottle.

I then ended up play testing the milk truck and bottles a few times to see how the suspension worked with additional mass at the back - I ended up reducing the mass of each individual bottle as too much weight did some strange things to the movement of the vehicle and made it flip very violently.

Here's the video version if that's what you prefer:


Logged
JeremyNunns
Level 0
**


View Profile
« Reply #9 on: April 15, 2022, 03:11:59 AM »

Milkman - Scenery and Background Assets

Update
In this update I go through how I made some background / scenery assets.

Road Texture
The first thing I made was a graphic for the road in MS Paint. Essentially I just used colours I thought looked fairly realistic and created a grainy, dirty effect with the spray function. This took a bit of time but it was lots of fun. I'm aware there are probably more sophisticated tools out there but I'm familiar with paint and I thought it was relatively easy. I then painted the road surface with this texture, but had to learn how to apply the texture to the object in specific-sized tiles so that it didn't stretch in a weird way. I also realised the double yellow line in the middle of the road didn't make much sense, so went back and changed this. It was pretty fun just driving around.

House Models
I then started to create the terraced house models which are meant to contain the route of the milk truck by acting as a wall on either side of the road. I applied the skills I learnt from building the truck and created extrusions for 3 windows and a door. I didn't want everything to be 2D. For the roof, I spent absolutely ages. Hours just messing around with the terracotta tiles. I wanted to make them as random and unique as possible so that once I copied and pasted the houses it wouldn't look like the same house again and again. I started colouring them in, with pretty warm colours initially, and started playing around with duplicating them and adding features like bay windows on the ground floor and flipping the position of the door. Again, this was all so that there was a bit of variety on the street.

When I imported it into Unity I allocated glass to the window areas, but realised one of the windows wouldn't become transparent. Unfortunately it was an early mistake I made during the extrusion process of the windows and I didn't think it was worth the time to fix it. If anything, it gave each house a slightly more unique look. Again, I tried to randomise the little houses by making them different and random colours, including the roofs.

Grass and Sky
I played around with the landscape function and created a ground which added to the overall background so that it wasn't just a road which you fell off of. I thought it best to populate this with a green carpet of grass. Repeating patterns can look really weird and I just tried my best to make the grass texture as random as possible.

I also got rid of the fog and added some sunset light by messing around with the skybox. I thought this gave the environment a slightly more cheerful feel.

Wooden Crate
Finally, I previously deliberately left a raised void in the trailer, so that the bottles would stand higher and would fall out the trailer more easily. Well I had to fill this void and so created a wooden crate. Secondly, I created the texture of wood, using various shades of brown in wavy lines. I wasn't sure if this would work once reduced in size, but once I applied the texture I thought it looked pretty good.

Here's my usual video update:


« Last Edit: April 18, 2022, 08:54:43 PM by JeremyNunns » Logged
mobilelast
Level 2
**


View Profile WWW
« Reply #10 on: April 24, 2022, 03:50:16 PM »

Fun idea. Nice devlog too, you’re making great progress.

As far as I remember, wheel colliders were damn hard to get working properly. Physics engines are never accurate, and many times dealing with them is just trial and error. But on the other hand, they are also the source of the funniest glitches. And anyone who considers Blender to be user friendly, should do fine.

Physics and graphics look very good already. The concept immediately reminded me of Paperboy. Delivering (smashing physics-simulated bottles against walls) would be neat.
Logged

Avaruustaistelupeli (ATP) - a space combat game
- Free download from itch.io or IndieDB
- Dev diary here
JeremyNunns
Level 0
**


View Profile
« Reply #11 on: April 25, 2022, 06:07:58 AM »

Thank you!  Gentleman

Yes wheel colliders were a real pain - but the weird suspension is sort of part of the game now!

Haha Paperboy is old school! I think the gameplay here will just be crossing the line with either (i) the best time; or (ii) the most number of bottles in the trailer within a set time. Haven't quite decided which way to go yet (or perhaps the option to choose).

My next update will be about destructible bottles!
Logged
JeremyNunns
Level 0
**


View Profile
« Reply #12 on: April 26, 2022, 05:49:17 AM »

Destructible Milk Bottles, Particle Effects and Instantiation

Hello again Gentleman I managed to create a destructible milk bottle by slicing it up in Blender and used particle effects to show milk spilling out of the bottle.

Since a principal part of my game is trying to keep milk bottles on the trailer of the Milk truck, obviously I had to create the graphics for a situation where the milk bottles do fall off the truck. I sort of wanted it to be realistic, but also kind of cartoonish.

Who Broke my Milk Bottle?

I knew I had to create a destructible glass bottle, but I wasn't sure if there was code that could just split an object up or if you needed two versions of the same object: one normal and the other destructible. I figured out that you quite obviously needed two objects and so researched how to instantiate destructible object versions upon a triggering event (and there was a helpful Brackey's tutorial on this as well).

So in Blender, I studied how to use the knife tool and bisect tool, and well basically just started chopping away. I couldn't figure out how to use the bisect tool without deleting one portion of the object I was bisecting, so I ended up having to replicate the bisection on both sides of the object, which meant that the cuts weren't exactly lined up and so when I tried to match up the pieces again it didn't quite link together seamlessly. I wasn't too bothered by this because it was supposed to be a bunch of broken pieces of the bottle anyway.

So I imported it into Unity and added colliders onto the individual pieces, I decided to forego the whole mesh collider thing again and just built a bunch of box colliders onto the individual glass pieces. It didn't have to be that accurate because it was just a bunch of broken pieces.
So after that was done I thought the bottle shattering needed a few more broken bits of glass, as anyone who has broken glass at home would know.

Milk Milk Everywhere

I then tried to create hundreds of tiny white beads to represent the milk and see if it looked like milk is spilling everywhere. Didn't look great though and abandoned that idea.

So I looked everywhere for the right tutorial and eventually found a blood splatter tutorial by Aqsa Nadeem, who by the way does some great tutorials on particle effects, so check her stuff out. Her blood splatter was almost exactly what I was looking for but I wasn't sure it would work well in the environment I had built, I went ahead with it anyway.

I added the splatter component which triggers once the first droplet touches a surface. I had a lot of trouble with this, especially trying to make the splatter appear parallel to the ground and not perpendicular somehow. I experimented what the milk would look like if it hit a wall and it didn't look great. It also appeared to be occurring parallel to the ground but in mid-air. Even then, the amount and explosiveness of the milk was a bit much!

Abra Cadabra Script Wizard

The next thing I worked on was the script for the milk particle effect. I wrote some weird code at first like new Object = new because I thought I was dealing with pure C# functions, but turns out Unity has its own Instantiate function which had a number of options to choose from - I struggled a little at this point to enter the relevant parameters for the instantiation, in particular transform and rotation values. So I looked up how to fix my code and played around with quaternion values. I adjusted the rotation of one of the planes and it appeared to work! The milk was no longer spilling vertically. It was pretty cool.

The final step was to destroy the original milk bottle and replace it with the broken glass version. This was pretty simple - just required creating another GameObject variable called broken bottle and adding another instantiate function. What I wanted to do however was introduce a time delay so that the milk bottle would shatter a split second after it hit the ground. I had some C# difficulties with this and ended up messing around a lot, such that the code just did not function and the bottles did not replace itself with the broken glass version. I think I was getting too cute with creating a separate method for the instantiation of the broken glass, which was basically a waste of code.

No matter what I did with the script it didn't seem to work. I then figured out that I needed to create prefabs of the milk bottles, and assign the prefab models, not the original object itself. This way one need not literally create a new game object for each broken bottle.

I was pretty happy with the result, let me know if you are too! You can check out the video update here if you prefer:





Logged
JeremyNunns
Level 0
**


View Profile
« Reply #13 on: May 06, 2022, 08:09:07 PM »

Crashing Unity

I had originally planned on updating you all on the gameplay mechanics  Gentleman, but I came across an interesting issue which needed to be resolved before any proper playtesting could commence. Basically the broken milk bottles being instantiated were exploding exponentially and being called too many times.

I now had 32 bottles in the trailer, but every time the milk bottles smashed, there seemed to be an awful lot of glass! I took a rather unscientific approach of just counting distinct pieces of glass, i.e. the bottoms of the bottles, and once I reached more than 32, I knew something was off.

Since the problem appeared to be the sudden and excessive instantiation of the broken milk bottles and particle effect, I first looked to fix this in the code by somehow limiting how many times the broken bottles would be instantiated per frame. I thought this could be achieved by relying on a boolean value, so that the instantiation only happens for one frame before it switches itself off.

Tracking the culprit

Obviously there still appeared to be an issue, so I decided to add a debug.log to see if I could track the number of instantiations of broken bottles, bearing in mind that there should only be 32. Turns out there were 166! That's approximately 5x the number of broken bottles instantiated for each original milk bottle.

So I decided to get rid of the boolean solution. I had a feeling it had something to do with the collider somehow touching the same surface multiple times within a frame and therefore causing this exponential explosion of glass, but then I read somewhere that OnTriggerEnter is only called once by default, and someone else suggested that excessive colliders within objects could be the culprit.

So I first removed an arguably unnecessary box collider from the milk bottle itself, which represented the top tapered end of the milk bottle and cap. I just elongated the original one and made it slightly narrower. I then tested it again to see if it would work. Well, there did appear to be fewer versions of the broken bottles being instantiated, but I wasn't sure so I tried again and.. 355 instantiations! Something was obviously still wrong, even if it wasn't quite crashing the programme.

I thought for a while that perhaps delaying the broken glass instantiation from the particle effect of milk spillage would make it easier on the memory, but abandoned this idea because it wasn't really going to the root of the problem.

Tagging System?

I then got a hint online that it may have been due to the tagging system or tagging comparison code. But since I had currently required the broken bottles to instantiate upon collision with "Untagged" objects, basically all the objects in the world were "Untagged" except for the truck itself with other bottles. Obvious reason was that I did not want the rattling between the bottles to cause them to break in the back of the truck.

So I thought it was worth isolating and being specific about which objects the bottles were colliding with. I then created a new tag called "Ground", and set the asphalt road to this new tag of "Ground". I changed the code to only react when it collided with the Ground tagged object.

It worked! Perfect! No lag and the console was consistently showing that only 32 broken bottles were being instantiated.
I then applied a similar treatment to other objects in the game by removing unnecessary box colliders and changing the tags of specific objects in the game from "Untagged" to "Ground".

In retrospect, my suspicion is that the broken bottle pieces were also tagged as 'Untagged', which meant there was a bit of a chain reaction going on whenever a bunch of bottles exploded in close proximity.
 
As usual, a video of my latest shenanigans is here  Gentleman:


Logged
oldblood
Level 10
*****

...Not again.


View Profile
« Reply #14 on: May 07, 2022, 10:16:23 AM »

Just leaving a note to say that while I haven't commented, I have been enjoying your video devlogs. You have a great narration voice and make them very entertaining. I honestly didn't think a game about milk delivery would hold my attention, but your vlogs continually do so.
Logged

JeremyNunns
Level 0
**


View Profile
« Reply #15 on: May 09, 2022, 12:57:15 AM »

Just leaving a note to say that while I haven't commented, I have been enjoying your video devlogs. You have a great narration voice and make them very entertaining. I honestly didn't think a game about milk delivery would hold my attention, but your vlogs continually do so.

Really means a lot to read comments like this! My aim really with the DevLogs was to create a bit of a supportive community around my creative process of this awesome thing called games - so I'm really glad that you enjoy the DevLogs!

The game idea itself is admittedly a bit silly - and isn't exactly in my 'dream game' category (!) - but I thought it was an interesting way of delving into game development (in particular Unity, Blender, C# and other tools) without too much baggage of trying to make a passion project right off the bat. Hopefully it will actually turn out to be an entertaining game - but only time will tell!
Logged
Feardemic_Michal
Level 0
**


View Profile
« Reply #16 on: May 09, 2022, 03:49:47 AM »

I really like the premise, it's a creative idea  Coffee
Logged
Alain
Level 10
*****



View Profile WWW
« Reply #17 on: May 10, 2022, 05:26:20 AM »

Just leaving a note to say that while I haven't commented, I have been enjoying your video devlogs. You have a great narration voice and make them very entertaining. I honestly didn't think a game about milk delivery would hold my attention, but your vlogs continually do so.

Could not agree more, I've been lurking for a while and also enjoy your videos a lot Smiley
Logged

JeremyNunns
Level 0
**


View Profile
« Reply #18 on: May 12, 2022, 05:41:29 AM »


Could not agree more, I've been lurking for a while and also enjoy your videos a lot Smiley

Thank you Alain! Really liking what you're developing - love the style and looking forward to hearing more about the gameplay and testing etc
Logged
JeremyNunns
Level 0
**


View Profile
« Reply #19 on: June 20, 2022, 01:33:12 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):









Logged
Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic