Consulting, services, computer engineering. Implementation of technology solutions and support for businesses.

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Windows 11 v26080 Csharp First FPS creating a health pickup

 

Windows 11 v26100 Csharp First FPS creating a health pickup

Windows 11 v26100 Csharp First FPS creating a health pickup

 

 

 

Who am i / About the author...

ident021 180 facebook linkedin insider mcp learn metadatas sans cravate 128x128 autocolors 18052006 191123 mcse microsoft logomcsa microsoft logomcdba logo

since 2001, Insider since 2014.

 

Thanks to Microsoft EMEA GBS to let me participate in the year 2014 to the Insider project during my work. Then, This Windows test project, here in version 11, is based on the cycle of the insider Preview Builds, now in the Dev Channel: it is fun to test the new features. I can learn new features, validate them and share them with you :-)

Why game engines? Because as a system/network/database developer I only see consoles and screens with unfunny and ugly text. With game engines i can show you colors and animations, that's more fun and more eye-catching :)

This tutorial is made in a test environment, for pleasure and not to earn money. It is a non-profit project. It shows that the concept is possible, for a demontration, artistic, or educational purposes, school learning course. Don't test in a real environment in production. 

 

Windows 11 v26100 Csharp First FPS creating a health pickup : last articles

 

 

how to install Godot engine

Windows 11 v23619 testing with a Unity game engine Hello World

How to use signals with Godo Engine (Red bouncing ball)

A Paddle Game with Godot engine

Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC

Windows 11 v26080 Blender installation

Windows 11 v26100 Csharp First FPS building the level

 

Windows 11 v26100 Csharp First FPS creating a health pickup : object creation

 

The idea is simple: when the player will be hit by an enemy, its health bar will down a little. To refill the health bar, the player need to eat one or more health pickup depending of the damage taken. 

 

I create a capsule object :

Herarchy +> 3D object> Capsule

I name it Health_Pickup

I set the scale to 0.3 for the x, y, and z axes, and then switch to the Move tool and position

it near one of the barriers.

I create and attach a new yellow-colored Material to the Health_Pickup object.

In the material folder, i right click, create/ material/ and name it health_pickup_material

I select this new material in the material folder and look at its properties in the Inspector

I click on the color box next to the Albedo property and select the blue color R:255 G:255 B:0

I drag and drop the health_pickup_material object from the Project panel, onto the Health_Pickup GameObject in the hierarchy panel

I drag the Health_Pickup object from the Hierarchy pane into the Prefab folder.

 

Windows 11 v26100 Csharp First FPS creating a health pickup

 

Windows 11 v26100 Csharp First FPS creating a health pickup : code

 

I’ll use the transform component of the GameObject Capsule.

I create a new script inside the Scripts folder and i name this script Transformation(.cs)

I double click to open the script in Visual Studio Code.

 

Inside the class, i add :

    public int RotationSpeed = 100;

    private Transform itemTransform;

 

Inside the Start() method, i grab the object’s Transform component and i assign it to the itemTransform variable :

       itemTransform = this.GetComponent<Transform>();

 

Inside the Update() method, i invoke the Rotate method on the X component so, i add :

    itemTransform.Rotate(RotationSpeed * Time.deltaTime, 0, 0);

 

 

The ItemRotation.cs script looks like :

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemRotation : MonoBehaviour
{

public int RotationSpeed = 100;
private Transform itemTransform;

// Start is called before the first frame update
void Start()
{

itemTransform = this.GetComponent<Transform>();

}

// Update is called once per frame
void Update()
{

itemTransform.Rotate(RotationSpeed * Time.deltaTime, 0, 0);

}
}

 

In the Assets/Prefabs folder, i select the Health_Pickup.prefab

Inspector/Add component/Scripts/ And I select the ItemRotation.cs

 

Windows 11 v26100 Csharp First FPS creating a health pickup : duplicate

 

Because i want a health pickup in each corner, i need to add 3 more health pickups.

I select the health pickup in the Hierarchy

Right click , duplicate

I rename it health pickup 2 and put it in the second tower turret.

I repeat those steps 2 others time for the 2 other turrets.

 

Windows 11 v26100 Csharp First FPS creating a health pickup X4

 

Great! Now i have four health pickups situated in each of the corners of the map. When the player will be injured by an enemy, it will run in the corner, eat the health pickup and refill its health bar.

 

Windows 11 v26100 Csharp First FPS creating a health pickup : new camera

 

One camera is focused on the arena and i want now another camera focused on a health pickup to see it in action

 

I create another camera

Sample Scene/ Game Object / Camera

I rename it Camera2

Inspector / Target display / Display2

Now, when i select Display2 in the Game tab, i see what Camera2 see.

 

Windows 11 v26100 Csharp First FPS creating a health pickup : item picked up

 

I create a C# script in the script folder named ItemBehavior

I edit this script with Visual Studio Code.

I drag and drop this new script on the Health_pickup object in Hierarchy panel

The GameObject Health_pickup shows in the Inspector 2 scripts referenced.

 

Windows 11 v26100 Csharp First FPS creating a health pickup script item behavior

 

I click on these 3 dots of Item Behavior script, added component/Apply to prefab ‘Health_pickup’

Here is the C# code added to the ItemBehavior.cs script :

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemBehavior : MonoBehaviour
{

void OnCollisionEnter(Collision collision)
{
// When another object runs into the Item Prefab, Unity automatically calls the OnCollisionEnter method

if(collision.gameObject.name == "Player")
{
// The Collision class has a property, called gameObject, that holds a reference to the colliding GameObject’s Collider


Destroy(this.transform.gameObject);
// removes the object from the scene

Debug.Log("Item collected!");
// print in the debug console
}
}
}

I click on Play, F5, and with the arrow key i move the player to the health pickup.

In the console i see :

Item collected!

UnityEngine.Debug:Log (object)

ItemBehavior:OnCollisionEnter (UnityEngine.Collision) (at Assets/Scripts/ItemBehavior.cs:15)

UnityEngine.Physics:OnSceneContact (UnityEngine.PhysicsScene,intptr,int)

And the capsule disappears, great!

Colliders with the isTrigger property disabled. If this property is enabled, the collider will be ignored by the physics engine, but will trigger events OnTriggerEnter, OnTriggerExit, and OnTriggerStay notifications. Interresting to use if the player enters an enemy zone, the enemy will be notified and attack the player…

 

 

 

Windows 11 v26100 Csharp First FPS creating a health pickup : conclusion done

 

Done! i created the project Csharp First FPS creating a health pickup, on windows 11 version 26100.

Windows 11 v26100 Csharp First FPS creating a health pickup

Windows 11 v26100 Csharp First FPS creating a health pickup : need help ?

 

Need help with Windows 11 or C# with Unity?

Fill this form or send an email on loic @consultingit.fr or This email address is being protected from spambots. You need JavaScript enabled to view it. and i will be happy to help you :)