Western Shooter Devblog #4 | Introduction to Physics in Unity

Ethan Bristowe
3 min readApr 30, 2021

--

As fun as it is to shoot at a blank wall for 5 minutes, I think it’s time we add some enemies for Cubert to shoot at!

Meet Rectangela, she’s just a duplicated Cubert but red because she’s evil.

Just like the bullets, we want enemies to move by themselves, but they should only be destroyed when they get shot by Cubert, not by a timer. So how do we do that?

With physics of course!

For the movement, I want our enemy to walk back and forth on either side of the lane in front of Cubert. I was able to combine features from both how the player moves and how the bullet moves to make something new!

I took the same exact script from the player movement, but gave the _direction variable a different purpose.

Instead of representing where the character is going based off of Player input, I’m going to assign it a different direction based off of what side of the lane it has reached. I also took these if statement from the player script, but instead of stopping the character in place, it changes their direction!

And now we have enemy movement!

But we need our enemy script and our bullet script to communicate with eachother only when their Colliders… well… Collide. So to do this, we are going to set our enemy collider to a trigger and then in the enemy scripts we’ll use an OnTriggerEnter() method to detect Collisions.

But not ALL collisions! We only want our enemy to react to the bullet collider, so we’ll create a simple tag and create an if statement to only destroy the enemy gameobject when it collides with anything tagged as our Pistol Round.

And their we go! Might look a little anti-climactic, but we got the function done! And in record time!

However, this isn’t meant to just be target practice for Cubert, it’s time Rectangela starts shooting back! Next devblog we are gonna develop the enemy shooting behavior!

--

--

Ethan Bristowe
Ethan Bristowe

Written by Ethan Bristowe

Passionate self taught game developer excited to learn and share everything I can about game development!

No responses yet