Showing posts with label game scripting. Show all posts
Showing posts with label game scripting. Show all posts

Tuesday, March 5, 2019

Patrolling Enemy Tutorial



One of the most-requested features I get asked about in the Game Design classes I teach is how to make a patrolling enemy that will "spot" the player. I've been making a lot of video tutorials lately, but for this one I've decided to make a text tutorial. So here it is, how to set up a basic patrolling enemy that will "spot" the player.


Patrolling Enemy
Download Enemy Character and Animations from Mixamo
  1. Log in or sign up
  2. From the Characters tab, select a character you like
  3. From the Animations tab, select a walk cycle
  4. Check the box for In Place
  5. Download as FBX for Unity
  6. Download a second time for Collada, which gives a .zip folder with the texture files
  7. Find a second animation, such as an Attack, and download as FBX for Unity
  8. Import the FBX and the texture files to Unity
Setup and Bake NavMesh in Unity
  1. Create a Ground Plane in a new empty scene
  2. Open the navigation panel: Window > AI > Navigation
  3. Select the Ground Plane and mark it as Navigation Static and Walkable
  4. Create some obstacles for the enemy to walk around, it’s a good idea to change the color of the ground and obstacle so you can see what you’re doing
  5. Marke the obstacles as Navigation Static and Not Walkable
  6. Select the Bake tab and choose Bake
Create NavMeshAgent and Player Character Controller
  1. Create a Character Controller for the player, or download the Unity Standard Assets FPS controller and place it in your scene (if using the FPS controller, be sure it is above the ground)
  2. Create a 3D Capsule object in your scene and give it a NavMeshAgent Component
  3. Create a new C# Script called Enemy and attach it to the Capsule
  4. Open the script and create a Public Array of GameObjects
  5. Back in the Unity scene, select the Capsule and set the public array size to 3
  6. Create 3 Empty GameObjects and place them around the scene (these will be the waypoints for the enemy to walk to)
  7. Drag each of the waypoints into one of the GameObject slots on the Capsule
  8. Back in the Enemy script, add using UnityEngine.AI to the top of the script, create a private NavMeshAgent and in the Start function give it a reference to the NavMeshAgent component
  9. Create a private int to keep track of the current waypoint, then in the Start and Update functions specify the desired waypoint for the NavMeshAgent.destination
  10. Save and test and you will see the enemy walk between each of the waypoints until it reaches the end of the patrol path...update the script so the patrol loops by resetting currentWaypoint to 0 when you reach the end
  11. Test again to ensure the enemy loops on the patrol path
Link Character Model and Animation to NavMeshAgent
  1. Drag the enemy character model onto the Capsule to make it a child and turn off the Mesh Renderer on the Capsule
  2. Select the enemy character model in the scene and drag the Walking animation onto the Animator component

    Note that it creates an Animator Controller in your project folder when you do this
  3. Apply the textures to the character Geo
  4. Select the walking animation and choose Edit
  5. Check the box for Loop Time and Apply
  6. Test again to see the character walking along the patrol path. While testing, check to ensure the character’s feet are making contact with the ground
Script Enemy Vision and Attack Trigger
  1. Attach a Box Collider component to the enemy Capsule and mark it as Is Trigger and position and scale it to represent the view area of the enemy
  2. Add an OnTriggerStay function to your Enemy script to check for the Player character
  3. Be sure to tag the player as Player
  4. You may want to expand your play space to test this new functionality, be sure to rebake the NavMesh if you do
  5. Open the Animator wind from Window > Animation > Animator
  6. Select the animated enemy character and drag the Attack animation into the Animator
  7. On the Parameters tab make a new Trigger called ‘Attack’
  8. Right click on the Walking animation and make a transition to the Attack animation
  9. Make a corresponding transition from the Attack to the Walking animation
  10. Select the transition to the Attack and add a condition for the Attack trigger you made before and uncheck the box for Has Exit Time
  11. If the enemy is within range of the player, set the target destination to the enemy’s current position and trigger the attack animation, otherwise use the player’s position as the destination
Summary
After completing these steps, you should have a patrolling character that will interrupt it’s patrol to follow the player if the player gets in her view range, and if the enemy gets close enough to the player while following she will attack. If the player gets out of the view range of the enemy she will return to her patrol.

Thanks for reading, and if you've found this tutorial helpful I hope you will subscribe here and on my Youtube channel: www.youtube.com/c/AstireGames

Sunday, March 25, 2018

Game Dev Tutorials

Many game devs get their start from following tutorials, because even though there has been a surge of game-related classes and programs in colleges and schools, a lot of the content taught is overarching principles of design and workflows. Hands-on how-to material is still largely dominated by online tutorials.

Tutorials are a great way to learn how to build specific mechanics within specific engines or frameworks, and can be beneficial even for seasoned developers. Often tutorials cover not only scripting, but also art asset creation, world building, animation, and effects...you can make a complete game from scratch by following a tutorial, or you can pick up bits and pieces to fill in knowledge games.

For the past year I've been working on tutorializing content that I teach in classes, workshops, and camps. This is beneficial to anyone participating in my classes because it means they can work at their own pace and continue to learn even when I am not there, and it also allows people from all around the world to experience this content because it is available online (and much of it is free).

If you are an aspiring game developer, or if you teach game dev classes or workshops, I hope you will consider checking out my suite of tutorials. Here is a rundown of what I have to offer (all tutorials use the Unity game engine):


 

This tutorial covers everything you need to know about using 2D sprites in Unity, either for 2D game objects  animated for 2D gameplay, or for UI elements in a 2D or 3D game. Topics covered include sprite sheets, color, sliders, and rotation/scale/position animations.



Unity has a built-in physics engine that allows you to quickly and easily animate objects using physical properties such as force, torque, friction, mass, and bounciness. In this tutorial you will learn how to use these different properties to create a wide range of movement and behavior for physics-based objects in both 3D and 2D.



In addition to rigidbody physics, Unity also supports cloth physics. This tutorial covers the basics for setting up an object to behave like cloth, including constraints, stiffness, stretchiness, and animated force for wind and gravity.




This tutorial breaks down the most common features of Unity's Particle System component, including sprite sheets, velocity over lifetime, color over lifetime, trails, emissions, and bursts.



One of the biggest distinguishing factors from a student game and a professionally developed game is the "juiciness" of the interactions. The hallmark of game design is making the game "feel good" and so much of that good feeling comes from making things feel reactive to the player's actions. This tutorial covers specifically using particles and sound effects to make the interactions juicy.



This tutorial is available on Pluralsight and covers the full pipeline for creating your first game prototype. It covers whiteboxing, importing assets, building a level, adding animations, particles, lighting, and sounds, and making it a complete experience with a UI start screen and win/lose screens.


https://www.pluralsight.com/courses/unity-vr-fundamentals

This tutorial is specifically intended for aspiring VR developers, and gives the basics of getting started with room-scale VR on the Vive, and mobile VR.

Please subscribe to our youtube channel for more tutorials, gameplay videos, and other fun things! Thanks for reading!

Friday, May 12, 2017

Game Prototyping in Unity, Part 3: Character Controller

This is a continuation of my blog post series about a tutorial I made called Game Prototyping in Unity available on Pluralsight. If you haven't read the previous entries, check out Part 1 on whiteboxing and Part 2 on art and sound integration.

There are many times when you don't need to make a character controller from scratch - a lot of simple first-person games use Unity's default first-person controller. You may also have a game where a character controller is not needed at all, such as a top-down strategy or puzzle game. Unity also has a third-person controller, which can work well in some circumstances. However, if you know the kind of behavior your want from your character controller, it is sometimes easier to make it yourself rather than trying to find one that suits your needs.

When designing a character controller, I like to first imagine how I want the player to see the world and the character. Do I want the player to be up close to the environment, seeing things first-hand?



Or do I want the player to see the character they are playing, see that character's animations and how that character interacts with the world?


For this game we will make a third-person character controller.

The next decision to make is deciding what the player's input will be to move the character. In some third-person games, the player clicks a point for the character to move there. In other games the player uses WASD or the controller joystick to move the character. There are also cases where the character moves automatically based on the context of the world - ie. moving toward an NPC to talk to them.

Since we want to offer controller support for this game, it makes the most sense to use WASD since that has a direct mapping to the controller joystick.

We also need to ask ourselves "Can the player control the camera?"

In some third-person games the camera automatically follows the character with no input from the user. In other games, the user controls the camera with the mouse or with the right joystick. We will use the latter option.

For this character controller I've decided to use Unity's NavMesh because it automatically handles keeping the player on the ground and smooth gradual movement. The NavMesh allows you to determine if objects in the scene are "walkable" - meaning the top of the object is a surface the player can walk on - or "not walkable" - meaning that object is an obstacle or in some way inaccessible. Once you'd baked the NavMesh you can see what areas the player can walk on as a blue surface.


The scripting part is fairly simple, we just need to attach a NavMeshAgent component to our character and then tell it which was to move.


Unity also comes with a "Smooth Follow" script which can be attached to the camera and set to target the character, that was the camera moves when the character moves. We can also use Unity's "Simple Mouse Rotator" script to give the player control of the camera direction.


The final step is going to be giving the player a ranged attack. A ranged attack can be anything that is fired from a distance such as a gun or a spell. I think a spell would work better in the kind of world I am creating, so I will make a projectile object with a particle effect like a spell, and put it in a folder called "Resources" (it's important this one is spelled correctly, because this is the only folder Unity can load from at runtime).


Then we need to instantiate that projectile when the player fires.


Then on the projectile itself we want to tell it to move forward on every frame (since we are instantiating it facing the direction we want it to move, it should only ever move forward).


And that's it! Now we have a third-person character controller that moves with WASD and uses the mouse to look around, and can cast a spell at enemies.


Check out the full video tutorial on Pluralsight! Section 4 will cover scripted interactions.