The first day of getting started with Unity. Familiar with the interface and object movement
This is my first day of learning Unity, and I summed up some experience with the tank battle tutorial of Siki Academy.
This article talks about the Unity interface and character movement.
About the interface
project
involves all the art models (scene, props, main body) required by developers. The produced animation can be packaged and placed in a folder named Animation, and the animation controller can also be placed in a folder named Animation Controller In the folder of the scene character, the script design of the scene character can be placed in the Scripts, and the scene in the Hierarchy can be placed in the Map folder.- Drag and drop the materials in the project into the
Hierarchy
, which means that these materials can be used in thescence
, that is, the scene. The size of the material can be set by adjusting the value of xyzScale X3 Y3 Z3
.- If you want to set the same (size) value in multiple clips, you can copy and paste the first clip and drag the clip you want to change into the
Sprite
of theSprite Renderer
. - If you want to render an animation composed of multiple materials in
project
inHierarchy
,shift
select multiple materials, drag them intoHierarchy
, and name it the name of the animation.
- If you want to set the same (size) value in multiple clips, you can copy and paste the first clip and drag the clip you want to change into the
Scene
is the presentation of the material in the game. You can freely drag the selected material with the mouse, and right-click to move the overall camera.When you want to set the object to 1m and don’t know how to adjust the size of the material, right click in Hierarchy -> 3D Object -> Cube, you can get a cube with a length of 1m * 1m * 1m.
Game
shows the result (animation, character control) that will be rendered after pressing the play key ▶️.
Character movement code analysis
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float moveSpeed = 3;//Set the character movement speed: 3m
//Sprite Renderer is used to render and display images in Scene, abbreviated as sr
private SpriteRenderer sr;
//Up right down left, pictures in different directions are represented by different sequences []
public Sprite[] tankSprite;
//Awake will be called when the script is instantiated (regardless of whether the script is enabled or not)
private void Awake(){
sr = GetComponent<SpriteRenderer>();
}
void Start()
{
}
// Update is called once per frame
void Update()
{
//"Horizontal" cannot be misspelled, otherwise it will not be recognized.
//identify the keyboard
float h = Input.GetAxisRaw("Horizontal");
//transform is used to control the position of the object, right is horizontal
//Time.delatTime represents the time (in seconds) it took to complete the previous frame
//Use this to apply a transformation to the GameObject via world coordinates. This means that the GameObject's transform is changed through world space instead of the GameObject's local space
transform.Translate(Vector3.right * h * moveSpeed *Time.deltaTime, Space.World);
//If the value of h on the left is -1 and the value on the right is 1, you can judge when the tank turns
if (h < 0){
sr.sprite = tankSprite[3];
}
else if (h > 0){
sr.sprite = tankSprite[1];
}
//When v is greater than 1, go up, otherwise go down
float v = Input.GetAxisRaw("Vertical");
transform.Translate(Vector3.up * v * moveSpeed * Time.deltaTime, Space.World);
if (v < 0){
sr.sprite = tankSprite[2];
}
else if (v > 0){
sr.sprite = tankSprite[0];
}
}
}