laitimes

Learn game development and be your own Krypton guy (Unity - Video /& Character Control)

author:Convert to a blank resume

I suddenly realized that learning seemed to be more fun than playing games.

As for why I suddenly had this idea, please don't ask me, ask about the excellent matchmaking mechanics of competitive games

Learn game development and be your own Krypton guy (Unity - Video /& Character Control)

Come to think of it, all I can get from the game lately is the torture of my teammates and a wallet that keeps drying up.

And learning, I can gain knowledge from it, which can improve myself for example, find a better job and earn more money. Or learn game development like I did, and build a game like Valorant.

Then replace all the opponents in the game with simple man-machine.

Learn game development and be your own Krypton guy (Unity - Video /& Character Control)

Of course, I still like games very much, not only just like to play, but also to understand the story behind it and so on.

Excellent storylines like The Witcher III and Red Dead Redemption 2 have touched me like nowhere else. Even a game like UnderTale with a simpler drawing style keeps me engaged for a long time.

Having said that, I have to play a game again and come back to study.

Learn game development and be your own Krypton guy (Unity - Video /& Character Control)

Back to business

Today, because the article was written relatively early (mainly because I was driven back by the game), I will learn two parts, which are the video playback in the game and how to make the characters in Unity move.

Let's start today's ordeal!

I. Video Playback (P36)

The video playback and the audio playback mentioned in the previous note are actually similar in the code, so find an MP4 file here to demonstrate.

The method of putting a video into Unity is as simple as right-clicking below the project view on the right→ selecting Show in Explorer→ and dragging the video file into the Asset folder

Learn game development and be your own Krypton guy (Unity - Video /& Character Control)

1-1. Play in the game scene

Right-click in the project view to create a renderer texture and then create a GameObject, for example to play on a flat surface.

Learn game development and be your own Krypton guy (Unity - Video /& Character Control)

After creating the plane, add a component called VideoPlayer, which is much like the previously mentioned component for playing audio, AudioSource. The clip that plays audio in Unity is called AudioClip, and the clip that plays video is called VideoClip.

Learn game development and be your own Krypton guy (Unity - Video /& Character Control)

1-1.1 Videoplayer component settings

Back to the videoplayer component on the plane, just drag and drop the video file directly into the video clip. Then change to the renderer texture in the rendering mode below, dragging the renderer texture to the target texture.

1

Learn game development and be your own Krypton guy (Unity - Video /& Character Control)

After this step, everything in the video will appear in the renderer texture by dragging the texture onto the object you want to play.

Learn game development and be your own Krypton guy (Unity - Video /& Character Control)

1-2. UI playback mode

Of course, there are other ways to play it, creating a UI: the original image → dragging the renderer texture into the texture of the RawImage

Learn game development and be your own Krypton guy (Unity - Video /& Character Control)

1-3. Script control video playback

Create a C# script named VideoTest and mount the script on the same object that has a VideoPlayer.

Because there is no Video in the script, a namespace needs to be referenced:

using UnityEngine.Video;

Then you can declare below:

private video player player;

Next get the component player:

player = GetComponent<VideoPlayer>();

Once you have acquired the components, you can take control:

if(Input.GetMouseButtonDown(0));

{

if(play.isPlaying){

play. Pause();}

}

In fact, the principle is the same as that of audio playback, and you can enter commands such as pause, stop, start playback, etc. at will.

Stage code display:

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

II. Role Control (P37)

Finally! To start learning character control, there are also many kinds of character control in Unity, there are three types of character control, just choose one

  • Buy it in the Unity Asset Store and use it when you take it
  • Unity has its own character controller to work with
  • Later, learn the physics system and then use the physics system to operate

For someone like me who doesn't have the money and hasn't learned physics yet, this will honestly operate with Unity's own character controller.

2-1. Role control components

Create a plane and place it on the world axis (0,0,0), making sure it is oriented correctly (red x-axis is right, blue z-axis is front)

Then create a random object as a character, place it on a plane, and then carry a component called the Character Controller (keep the default settings)

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

2-2. Control roles in scripts

Create a script called PlayerControl and mount it on the object.

The first step is to declare it in the script and then get this component:

Private CharacterController player;

Next get the components (in start):

player = GetComponent<CharacterController>();

After getting the component, you can write mobile code (in Update)

Before writing code, know that characters in Unity generally move according to two axes, one horizontal and one vertical

2-2.1 Horizontal axis

As mentioned in the previous virtual axis, left (-1), right (1) and still (0)

float horizontal = Input.GetAxis("Horizontal");

2-2.2 Vertical axis

float vertical = Input.GetAxis("Vertical");

2-2.3 Create as a direction vector

After obtaining the two axes, you can know which buttons are pressed (pressing A means going left)

Vector3 dir = new Vector3 (horizontal,0,vertical);

There are generally 3 vectors in Vector3, which are X, Y, and Z

Here I want the horizontal axis horizontal to affect the X left and right movement, the vertical axis Vertical to affect the Z axis movement, as for the Y axis of the heaven and earth things do not care.

By the way, draw all the vectors, and use their own position if they are positioned.

Debug.DrawRay(transform.position,dir,Color.red)

Then go back to Run in Unity, where you have to click on the Game View window below.

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

In fact, you can see that after pressing WASD in any direction on the keyboard, a red ray will come out.

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

2-3. Move in the direction

There are two approaches in Unity: one is move, which is not affected by gravity; The other is SimpleMove, which is influenced by gravity.

player. SimpleMove(dir);

The dir here is to make the object move in the direction, that is, when pressing which key on the keyboard in which direction

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

You can see that in Unity running, pressing WASD at will move up, down, left, right.

Of course, looking at this movement speed is a little slow, it seems that you can add a speed here:

player. SimpleMove(dir*2);

Let it move twice as fast

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

And, of course, the gravity mentioned above:

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

That's the end of today's lesson!

Of course, for me, I have to prepare to start writing the content of the second public account tonight, and then post another public account when I go back. Another public account is mainly about non-software related knowledge, such as the installed computer accessories I have been studying these days.

I'm going to eat the fried cow river of my heart, see you in the next note, bye!

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

Original Course Link:

https://www.bilibili.com/video/BV1gQ4y1e7SS/?spm_id_from=333.337.search-card.all.click

Unity Video Player:

https://blog.csdn.net/weixin_43367805/article/details/93178830

Unity Physical System:

https://docs.unity3d.com/cn/2021.3/Manual/PhysicsSection.html

Code summary for this issue

Video section:

Role controller section:

I suddenly realized that learning seemed to be more fun than playing games.

As for why I suddenly had this idea, please don't ask me, ask about the excellent matchmaking mechanics of competitive games

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

Come to think of it, all I can get from the game lately is the torture of my teammates and a wallet that keeps drying up.

And learning, I can gain knowledge from it, which can improve myself for example, find a better job and earn more money. Or learn game development like I did, and build a game like Valorant.

Then replace all the opponents in the game with simple man-machine.

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

Of course, I still like games very much, not only just like to play, but also to understand the story behind it and so on.

Excellent storytelling, games like The Witcher III and Red 2 have touched me like nowhere else. Even a game like UnderTale with a simpler drawing style keeps me engaged for a long time.

Having said that, I have to play a game again and come back to study.

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

Back to business

Today, because the article was written relatively early (mainly because I was driven back by the game), I will learn two parts, which are the video playback in the game and how to make the characters in Unity move.

Let's start today's ordeal!

I. Video Playback (P36)

The video playback and the audio playback mentioned in the previous note are actually similar in the code, so find an MP4 file here to demonstrate.

The method of putting a video into Unity is as simple as right-clicking below the project view on the right→ selecting Show in Explorer→ and dragging the video file into the Asset folder

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

1-1. Play in the game scene

Right-click in the project view to create a renderer texture and then create a GameObject, for example to play on a flat surface.

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

After creating the plane, add a component called VideoPlayer, which is much like the previously mentioned component for playing audio, AudioSource. The clip that plays audio in Unity is called AudioClip, and the clip that plays video is called VideoClip.

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

1-1.1 Videoplayer component settings

Back to the videoplayer component on the plane, just drag and drop the video file directly into the video clip. Then change to the renderer texture in the rendering mode below, dragging the renderer texture to the target texture.

1

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

After this step, everything in the video will appear in the renderer texture by dragging the texture onto the object you want to play.

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

1-2. UI playback mode

Of course, there are other ways to play it, creating a UI: the original image → dragging the renderer texture into the texture of the RawImage

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

1-3. Script control video playback

Create a C# script named VideoTest and mount the script on the same object that has a VideoPlayer.

Because there is no Video in the script, a namespace needs to be referenced:

using UnityEngine.Video;

Then you can declare below:

private video player player;

Next get the component player:

player = GetComponent<VideoPlayer>();

Once you have acquired the components, you can take control:

if(Input.GetMouseButtonDown(0));

{

if(play.isPlaying){

play. Pause();}

}

In fact, the principle is the same as that of audio playback, and you can enter commands such as pause, stop, start playback, etc. at will.

Stage code display:

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

II. Role Control (P37)

Finally! To start learning character control, there are also many kinds of character control in Unity, there are three types of character control, just choose one

  • Buy it in the Unity Asset Store and use it when you take it
  • Unity has its own character controller to work with
  • Later, learn the physics system and then use the physics system to operate

For someone like me who doesn't have the money and hasn't learned physics yet, this will honestly operate with Unity's own character controller.

2-1. Role control components

Create a plane and place it on the world axis (0,0,0), making sure it is oriented correctly (red x-axis is right, blue z-axis is front)

Then create a random object as a character, place it on a plane, and then carry a component called the Character Controller (keep the default settings)

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

2-2. Control roles in scripts

Create a script called PlayerControl and mount it on the object.

The first step is to declare it in the script and then get this component:

Private CharacterController player;

Next get the components (in start):

player = GetComponent<CharacterController>();

After getting the component, you can write mobile code (in Update)

Before writing code, know that characters in Unity generally move according to two axes, one horizontal and one vertical

2-2.1 Horizontal axis

As mentioned in the previous virtual axis, left (-1), right (1) and still (0)

float horizontal = Input.GetAxis("Horizontal");

2-2.2 Vertical axis

float vertical = Input.GetAxis("Vertical");

2-2.3 Create as a direction vector

After obtaining the two axes, you can know which buttons are pressed (pressing A means going left)

Vector3 dir = new Vector3 (horizontal,0,vertical);

There are generally 3 vectors in Vector3, which are X, Y, and Z

Here I want the horizontal axis horizontal to affect the X left and right movement, the vertical axis Vertical to affect the Z axis movement, as for the Y axis of the heaven and earth things do not care.

By the way, draw all the vectors, and use their own position if they are positioned.

Debug.DrawRay(transform.position,dir,Color.red)

Then go back to Run in Unity, where you have to click on the Game View window below.

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

In fact, you can see that after pressing WASD in any direction on the keyboard, a red ray will come out.

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

2-3. Move in the direction

There are two approaches in Unity: one is move, which is not affected by gravity; The other is SimpleMove, which is influenced by gravity.

player. SimpleMove(dir);

The dir here is to make the object move in the direction, that is, when pressing which key on the keyboard in which direction

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

You can see that in Unity running, pressing WASD at will move up, down, left, right.

Of course, looking at this movement speed is a little slow, it seems that you can add a speed here:

player. SimpleMove(dir*2);

Let it move twice as fast

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

And, of course, the gravity mentioned above:

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

That's the end of today's lesson!

Of course, for me, I have to prepare to start writing the content of the second public account tonight, and then post another public account when I go back. Another public account is mainly about non-software related knowledge, such as the installed computer accessories I have been studying these days.

I'm going to eat the fried cow river of my heart, see you in the next note, bye!

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

Original Course Link:

https://www.bilibili.com/video/BV1gQ4y1e7SS/?spm_id_from=333.337.search-card.all.click

Unity Video Player:

https://blog.csdn.net/weixin_43367805/article/details/93178830

Unity Physical System:

https://docs.unity3d.com/cn/2021.3/Manual/PhysicsSection.html

Code summary for this issue

Video section:

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)

Role controller section:

Learn game development and be your own Krypton guy (Unity - Video /&amp; Character Control)