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 Scripting camera behavior to follow the player

 

Windows 11 v26100 Csharp First FPS Scripting camera behavior to follow the player

 

Windows 11 v26100 Csharp First FPS Scripting camera behavior to follow the player

 

 

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 Scripting camera behavior to follow the player : 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

Windows 11 v26100 Csharp First FPS moving the player

 

Windows 11 v26100 Csharp First FPS Scripting camera behavior to follow the player : goal

 

Imagine we have two objects in our game: a player capsule and a camera. We want the camera to follow the player smoothly. The trick here is to make the camera a child of the player capsule. When an object is a child of another, its position and rotation are linked to the parent. So, as the player moves or rotates, the camera will automatically follow suit.

However, there’s a small hiccup. If we directly parent the camera to the player, any movement or rotation applied to the player will also affect the camera. Imagine the camera spinning wildly when the player does a somersault – not ideal!

What we really want is for the camera to maintain a fixed distance behind the player and always face the player, no matter what crazy stunts they’re pulling. Luckily, we can achieve this by manipulating the camera’s position and rotation relative to the player using methods from the Transform class.

 

 

Windows 11 v26100 Csharp First FPS Scripting camera behavior to follow the player : script

 

 

I create a new C# script in the Scripts folder, i name it CameraBehavior

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class CameraBehavior : MonoBehaviour

{

    public Vector3 CamOffset = new Vector3(0f, 1.2f, -2.6f);

    // distance between the Main Camera and the Player capsule

    private Transform _target;

    // give us access to the position, rotation, and scale of the player capsule

    // Start is called before the first frame update

    void Start()

    {

        _target = GameObject.Find("Player").transform;

        // Find the capsule player by name

        // The capsule’s x, y, and z positions are updated and stored in the _target variable every frame.

    }

    // Update is called once per frame

    void Update()

    {

    }

    void LateUpdate()

    {

        //executes after Update

        //this guarantees that _target has the most up-to-date position to reference.

        this.transform.position = _target.TransformPoint(CamOffset);

        //Sets the camera’s position to _target.TransformPoint(CamOffset) for every frame

        this.transform.LookAt(_target);

        //The LookAt method updates the capsule’s rotation at every frame, focusing on the Transform parameter we pass in

    }

}

I drag this C# script into Main Camera in the Hierarchy panel.

In the inspector, the public « cam offset » variable appears and i can modify the parameters if needed.

I select the tab « game » with main camera on display 1, and push play button F5

 

 

 

 

Windows 11 v26100 Csharp First FPS Scripting camera behavior to follow the player : conclusion done

 

Done! i created the project Csharp First FPS Scripting camera behavior to follow the player, on windows 11 version 26100.

Windows 11 v26100 Csharp First FPS Scripting camera behavior to follow the player

Windows 11 v26100 Csharp First FPS Scripting camera behavior to follow the player : 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 :)