Article directory
transform
Not only controls the rotation, position, and scaling of components, but also controls the parent-child relationship between components
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TransformTest : MonoBehaviour
{
void Start()
{
/*
//获取位置
Debug.Log(transform.position);
Debug.Log(transform.localPosition);
//获取旋转
Debug.Log(transform.rotation);
Debug.Log(transform.localRotation);
Debug.Log(transform.eulerAngles);
Debug.Log(transform.localEulerAngles);
//获取缩放
Debug.Log(transform.localScale);
//向量
Debug.Log(transform.forward);
Debug.Log(transform.right);
Debug.Log(transform.up);
*/
//父子关系
//获取父物体
GameObject parentGameObject = transform.parent.gameObject;
//子物体个数
Debug.Log(transform.childCount);
//解除与子物体的父子关系
transform.DetachChildren();
//获取子物体
Transform trans = transform.Find("Child");
trans = transform.GetChild(0);
//判断一个物体是不是另外一个物体的子物体
bool res = trans.IsChildOf(transform);
Debug.Log(res);
//设置为父物体
trans.SetParent(transform);
}
void Update()
{
//时时刻刻看向000点,敌人看向玩家,或者玩家看向物品
// transform.LookAt(Vector3.zero);
//旋转
//每帧旋转1度
// transform.Rotate(Vector3.up,1);
//绕某个物体旋转
//绕up轴旋转
// transform.RotateAround(Vector3.zero, Vector3.up,1);
//移动
//每一帧移动0.1距离
// transform.Translate(Vector3.forward * 0.1f);
}
}
camera camera
Perspective (3D)
Orthogonal (2D)
skybox
Need to download a little material from the mall
Camera weight (depth)
The higher the depth value, the higher the priority
Audio source
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioTest : MonoBehaviour
{
//AudioClip
public AudioClip music;
//播放器组件
private AudioSource player;
// Start is called before the first frame update
void Start()
{
player = GetComponent<AudioSource>();
//设定播放的音频片段
player.clip = music;
//循环
player.loop = true;
//音量
player.volume = 0.5f;
//播放
player.Play();
}
// Update is called once per frame
void Update()
{
//按空格切换声音的播放和暂停
if (Input.GetButtonDown("Jump"))
{
if (player.isPlaying)
{
// player.Pause(); //暂停播放
player.Stop();
}
else
{
// player.UnPause() //继续播放
player.Play(); //重头播放
}
}
//按鼠标左键播放声音
if (Input.GetMouseButtonDown(0))
{
// 这个播放是异步操作,非常方便
player.PlayOneShot(music);
}
}
}
video player
It’s not difficult to make a function of watching TV in the game. Let’s talk about the
plane model used for the screen, and paste it directly in front of the TV.
The video can be directly placed on the video player component on the plane, and the script is also hung on the plane.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
public class VideoTest : MonoBehaviour
{
private VideoPlayer player;
// Start is called before the first frame update
void Start()
{
player = GetComponent<VideoPlayer>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (player.isPlaying)
{
player.Pause();
Debug.Log("停止播放");
}
else
{
player.Play();
Debug.Log("继续播放");
}
}
}
}
load web url
Only urls ending in mp4 can be loaded, urls ending in html cannot be loaded, and html endings are video stream files, which cannot be read
test url
mp4 file url
character controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private CharacterController player;
// Start is called before the first frame update
void Start()
{
player = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
//水平轴
float horizontal = Input.GetAxis("Horizontal");
//垂直轴
float vertical = Input.GetAxis("Vertical");
//创建一个方向向量 ,水平轴horizontal影响x的位置,y轴是上天入地,不需要受影响(0),垂直轴vertical影响z的位置
Vector3 dir = new Vector3(horizontal,0,vertical);
Debug.DrawRay(transform.position,dir,Color.red);
//朝向该方向移动
player.SimpleMove(dir);
}
}
joint components
Hinge Joint
fix a joint
Spring Joint
Mount an object on another object
Fixed Joint
A force needs to be specified to break the joint
Animation、Animator
Old version (Animation)
Like pr, use keyframes to make