Unity 常规用法

发布时间 2023-10-18 18:24:55作者: thomas_blog

刚体 Rigidbody
控制游戏对象的物理行为

// 获取刚体属性
GetComponent<Rigidbody>();

聚焦
双击、F键

窗口控制
ALT+鼠标左键
鼠标中键
鼠标右键
滑轮
点击方向键移动物体
E键调整角度

场景视野
透视视野、平行视野

世界坐标原点
小球设置0/0/0坐标

Unity常用组件
Transform:控制游戏对象的位置、旋转和缩放。
Rigidbody:用于模拟物理行为,例如重力、碰撞和运动。
Collider:用于检测碰撞和触发事件,包括Box Collider、Sphere Collider、Capsule Collider等。
Renderer:渲染组件,用于将游戏对象显示在屏幕上,包括Sprite Renderer、Mesh Renderer等。
Animator:用于控制游戏对象的动画,包括关键帧动画、骨骼动画等。
Audio Source:用于播放音效和音乐。
Camera:控制视角和渲染场景的相机组件。
Light:用于照明场景的光源组件,包括Directional Light、Point Light、Spot Light等。
Particle System:粒子效果组件,用于创建各种粒子效果,例如火焰、烟雾、爆炸等。
NavMesh Agent:用于实现智能导航和路径寻找的组件。

材质
smoothness选项
用于指定材质的表面光滑度的属性。它可以影响材质的外观和反射性质

相机
拍一定范围内东西
一个场景可以有两个相机。一个相机渲染人物,一个相机渲染背景,渲染之后进行叠加

拖拽属性
public可以
private不可以

向量和力
Vector3.right: 1牛的力

施加力

Rigidbody rb = gameObject.GetComponent<Rigidbody>();
Vector3 force = new Vector3(10f, 0f, 0f);
rb.AddForce(force);

键盘控制

private float moveSpeed = 5f;

void Update()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical) * moveSpeed * Time.deltaTime;

    transform.Translate(movement);
}

GetAxis
Input.GetAxis("Horizontal") // 返回水平方向上的输入值(-1到1之间)
-1是左,1是右,渐变

旋转

transform.Rotate(Vector3.up); // 1次1°

脚本
Start: 开始执行一次
Update: 1S执行60次

碰撞检测
物体之间的碰撞

private void OnTriggerEnter(Collider other)
{
    Debug.Log("Trigger entered: " + other.gameObject.name);
}

private void OnCollisionEnter(Collision collision)
{
    Debug.Log("Collision occurred with: " + collision.gameObject.name);
}

private void OnCollisionStay(Collision collision)
{
    Debug.Log("Continuously colliding with: " + collision.gameObject.name);
}

标签
查找、筛选、识别、区分、组织、管理

相机跟随

void Update()
{
    transform.position = Vector3.Lerp(transform.position, target.position, followSpeed * Time.deltaTime);
}

触发检测
Is Trigger属性勾选
物体之间的进入和离开

void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Player"))
    {
        Debug.Log("Player entered the trigger!");
    }
}

void OnTriggerStay(Collider other)
{
    if (other.CompareTag("Enemy"))
    {
        Debug.Log("Enemy is still inside the trigger!");
    }
}

void OnTriggerExit(Collider other)
{
    if (other.CompareTag("PowerUp"))
    {
        Debug.Log("Power-up has left the trigger!");
    }
}


点光源: 灯泡
平行光源: 太阳光
聚光灯: 手电筒
区域灯: 设定形状

渲染
其他渲染软件: DirectX、OpenGL

预制体
预先制作好的物体
模板

打包
窗口模式:Player->Resolution and Presentation->Fullscreen Mode(Windowed)

步移
设定好步长
按设置步长拖拽移动

移动相机

transform.Translate

上一帧执行的时间

Time.deltaTime

Update执行时间
Project setting -> Time 可设置

void FixedUpdate()
{
    // 每固定帧的更新逻辑
}

鼠标按下判断

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Debug.Log("鼠标左键按下");
    }

    if (Input.GetMouseButtonDown(1))
    {
        Debug.Log("鼠标右键按下");
    }

    if (Input.GetMouseButtonDown(2))
    {
        Debug.Log("鼠标中键按下");
    }
}

添加冲力(开枪)

Rigidbody rb = GetComponent<Rigidbody>();
rb.velocity = new Vector3(1, 0, 0);