3D Survival Game Tutorial | Unity | Part 3: Selecting Items with Raycast & Creating Simple AI

3D Survival Game Tutorial | Unity | Part 3: Selecting Items with Raycast & Creating Simple AI

Introduction

In this episode, the presenter will show how to interact with objects and create basic AI for creatures that move around in the scene.

Creating a Pointer on the Screen

  • To indicate the center of the screen, create a small white circle image.
  • Import it as a sprite and apply compression.
  • Create a UI canvas and add an image component to it.
  • Set the source image to be the small white circle.
  • Change its scale to 0.05 so that it is smaller.
  • Position it at (0, 0, 0) and anchor it to the center of the canvas.

Adding Interaction Info UI Text

  • Create another UI text component called "Interaction Info."
  • Align it to the center and position it below the pointer.
  • Use this text component to display information about objects when hovering over them with the pointer.

Interacting with Objects

The presenter shows how to interact with objects by pointing at them using a pointer on the screen.

Creating Object Interaction Script

  • Create a new C# script called "ObjectInteraction."
  • Add code that detects when an object is being pointed at by checking if there is an object in front of the camera within a certain distance range.

Displaying Object Name

The presenter shows how to display an object's name when hovering over it with the pointer.

Updating ObjectInteraction Script

  • Update ObjectInteraction script by adding code that displays an object's name when hovering over it with the pointer.

Creating Basic AI for Creatures

The presenter shows how to create basic AI for creatures that move around in the scene.

Creating CreatureMovement Script

  • Create a new C# script called "CreatureMovement."
  • Add code that makes the creature move randomly around the scene.

Adding Creature Name Display

  • Update ObjectInteraction script by adding code that displays a creature's name when hovering over it with the pointer.

Disabling Hover Functionality

In this section, the instructor explains how to disable hover functionality when an object is not being hovered over and how to enable it when an object is being hovered over.

Creating a Selection Manager

  • A selection manager script is provided in the paste bin.
  • An empty object called "Selection Manager" is created and the selection manager script is dragged onto it.
  • The interaction game object for UI is dragged into the selection manager script so that it can be enabled and disabled when hovering over objects.

Using the Selection Manager Script

  • The update method in the selection manager script creates an array from the middle of the screen using camera.main.screenPointToRay and input.mousePosition.
  • The physics.Raycast method checks if this array hits any objects. If it does, information about that object's transform is stored in a variable called "selectionTransform".
  • To ensure that only specific objects are interacted with, a tag system can be used. Objects with certain tags can be selected while others are ignored.

Interactable Object Script

In this section, the instructor explains how to use an interactable object script to get the item name of an object and know if it is interactable.

Using Interactable Object Script

  • The interactable logic has a public string item name and a method that returns an item name which is a string.
  • If selection transform get component finds this component, then we can do something else. If not, we won't do anything.
  • Place the interactable object script on all trees by dragging it onto the prefab of art tree in the prefabs folder.
  • When pointing at one of these trees, it will find this component on them so it will know that it's actually an interactable object.
  • It takes the interaction text and changes its text to selection transform dot get component interactable object dot get item name.

Item Names

In this section, the instructor explains how to change item names for different objects.

Changing Item Names

  • We can type in "needs axe" or "use axe" for example.
  • Change the prefab birch 3 by typing "axe needed".
  • When we have an axe, we can approach this tree and then it will say click on the left mouse button to chop down or just press on something to interact.

Selection Manager

In this section, the instructor explains how to link selection manager with interaction and for UI so that they become active each time we hover over something and disabled when we hover on the ground.

Linking Selection Manager

  • We linked selection manager with interaction and for UI so it becomes active each time we hover over something.
  • It gets disabled when we hover on the ground.

Introduction to Using Creatures in Unity

In this section, the speaker introduces the concept of using creatures and animals in Unity to make a game look more realistic.

Using Free Assets from Asset Store

  • The speaker uses a free asset from the Asset Store called "White Rabbit" to demonstrate how to use creatures in Unity.
  • To use the rabbit asset, the speaker creates a prefab by dragging it into their scene and then into their prefabs folder.
  • The speaker creates two versions of the rabbit prefab: one small and one large.

Setting Up Animations

  • To set up animations for the rabbit, the speaker attaches an animator controller to it by dragging it onto the prefab.
  • The rabbit comes with several animations such as idle and run. The speaker sets up transitions between these animations using parameters like "is running".

Adding Components for Movement

  • To add movement components, the speaker adds a box collider and rigid body to prevent the rabbit from falling through terrain and enable gravity.
  • Finally, they add an AI movement script that can be used for any creature that wanders around.

Overview of AI Movement Script

In this section, the speaker explains the variables and methods used in an AI movement script for a bunny character.

Variables Used in the Script

  • Vector3 is used to determine the position where the bunny stops.
  • float variables are used for walk time and wait time.
  • public float variables are used for walk counter and weight counter.
  • bool variable is walking is used to check if the bunny is moving or not.
  • A random number generator is used to create different values for each clone of the prefab.

Choosing Direction

  • The method choose direction uses a random range to choose one of four directions (forward, left, right, back).
  • When a direction is chosen, is walking becomes true and the animation changes from idle to running/walking.

Animator Component

  • The animator component has a bool parameter called "is running" that needs to be set to true when the bunny is moving.

Understanding the Rabbit Movement Script

In this section, we will learn how to move the rabbit in different directions using a script.

Setting up the Animation

  • The animation is set to running and the walk counter is decreased by delta time.
  • The walk counter reduces one second every time and when it reaches zero, the movement stops.

Choosing Walk Direction

  • A switch statement checks which direction was chosen for walking.
  • The rabbit's rotation is changed to face the direction of movement.

Moving Forward

  • The rabbit moves forward on the blue axis using transform forward multiplied by speed and delta time.
  • Regardless of rotation, the rabbit always moves forward on the blue axis.

Stopping Movement

  • When the walk counter reaches zero, movement stops and position becomes static.

Creating a Script for Walking Creatures

In this section, the speaker explains how to create a script for walking creatures in Unity.

Writing the Script

  • The script places the creature in a stop position and changes its transform to stop position so it will no longer keep walking.
  • The animator is changed back to false so the creature stops moving and goes into idle animation.
  • The weight counter is reset, and when the walking variable is false, it waits until a certain amount of seconds have passed before choosing a direction again.
  • The script is placed on the creature, and public walk counter and weight counter variables are added to see how much time is left.

Testing the Script with Multiple Creatures

In this section, the speaker tests the script with multiple creatures.

Adding More Creatures

  • More creatures can be added by placing them as prefabs.
  • Each creature gets different values so they do not run or wait at the same time.

Adding Interactivity to Creatures

In this section, the speaker adds interactivity to creatures using an interactable object script.

Adding Interactable Object Script

  • The interactable object script is dragged onto each creature that needs interactivity.
  • When played, pointing at each creature displays their name as specified in the interactable object script.

Introduction to the Environment

In this section, the speaker introduces a new environment with different creatures and animals. The movement of these creatures is randomized, but they will always walk in a specific area.

Movement of Creatures

  • The movement of creatures is randomized.
  • They will always walk in a specific area.
  • If they reach the end of the world, they will fall down.

Picking Up Items and Creating an Inventory

In this section, the speaker discusses how to pick up items and create an inventory.

Picking Up Items

  • In the next episode, we will learn how to pick up items such as rocks or sticks.
  • To pick up items, we need to create a UI for an inventory.

Conclusion

  • Viewers can play around with these scripts and do whatever they want.
  • Please subscribe if you're still not subscribed to see all the newest episodes.

Turn any video into a summary like this

YouTube links, meetings, lectures. With transcripts, search, and chat.

Video description

#survivalgame #tutorial #unity In this tutorial series, we will create a 3D survival game with Unity & C# as the scripting language. We will start with basic FPS movement, inventory building, and crafting systems. Learn how to pick up items and more. Little by little, we are going to add different features that are common to survival open-world games. This series is a bit more advanced and fast-paced, but I will try to explain everything I do. Any questions are welcome. Selection Manager Script: https://gist.github.com/Mike-Schvedov/b55e70696ac449130f03b93d86b02bde InteractableObject Script: https://gist.github.com/Mike-Schvedov/16582b7edebcce9b071c908e0624203a Ai Movement Script: https://gist.github.com/Mike-Schvedov/c89b30c0a548385bfecc58d763db22ad White Rabbit free asset: https://assetstore.unity.com/packages/3d/characters/animals/white-rabbit-138709?aid=1011lu5No Sphere/Circle Sprite https://drive.google.com/file/d/1Z_OVHBIQdZOEW__hK8V-wY44w1MyIDFz/view?usp=drive_link Survival Series Playlist: https://youtube.com/playlist?list=PLtLToKUhgzwnk4U2eQYridNnObc2gqWo-&si=ZHLkDAE3UBpvmhvc šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾ Between my full-time job and my family life, I try to find free time to create content for this channel. You can support me and help this channel keep growing: https://paypal.me/MikeSchvedov220 šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾šŸ’¾ šŸ’» This Is My Development Setup (Affiliate): šŸ’» ============ Main Monitor: https://amzn.to/3M64qCJ Secondary Monitor: https://amzn.to/41Iu06A Graphics Card: https://amzn.to/3MpnXzd CPU: https://amzn.to/3I8nvCW RAM: https://amzn.to/42zqM6u Keyboard: https://amzn.to/3W5RFN4 Mouse: https://amzn.to/3nTPcZs Headphones: https://amzn.to/3pz0By5 Microphone: https://amzn.to/3OecJz3