Unity3D笔记1

第一个程序

Start() 游戏开始时运行

Update() 每一帧都会运行

Debug.log打印一段信息到Unity的控制台窗口

test.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    
    void Start()
    {
        Debug.Log("游戏开始运行");
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("游戏正在运行中...");   //显示日志
    }
}

注意:文件名要跟类名保持一致

//后的内容表示注释

c#文件后缀为.cs

通过监听键盘控制刚体对象移动

控制刚体对象移动

声明刚体对象

public Rigidbody 刚体对象名;

获取刚体组件并赋值给刚体对象

刚体对象名 = GetComponent<Rigidbody>(); //初始化内容

改变刚体的质量为10千克

刚体对象名.mass = 10f;

给刚体施加力

刚体对象名.AddForce(向量参数);  //帧更新内容

三维向量

涉及:物理-力(仅了解),数学-向量(已掌握)

向量代替力,向量和力都有方向有大小

常规向量

Vector.right | left | forword | back | up | down

新建向量

new Vector3(x,y,z)

Vector.forward == new Vector(0,0,1)

向量的运算

Vector3(1, 1, 1) * 3 -> Vector3(3, 3, 3)

控制刚体对象移动的速度

public float speed;
刚体对象名.AddForce(new Vector3(x,y,z) * speed);

监听键盘

float h = Input.GetAxis("Horizontal"); //监听ad键返回-1到1之间的数值
float v = Input.GetAxis("Vertical");   //监听ws键返回-1到1之间的数值

在Project Settings -> Input Manager -> Axes中可以查看其他按键的监听

通过监听键盘控制刚体对象移动

开始时定义刚体对象rd和速度speed,并获取操作对象的刚体组件

通过Input.GetAxis()函数监视adws按键并获取对应的力h、v

以向量h、v和速度speed通过刚体对象的AddForce()操作施加力

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public Rigidbody rd;
    public float speed;

    void Start()
    {
        rd = GetComponent<Rigidbody>();
    }

    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical"); 
        //为刚体对象施加监听键盘获得的力
        rd.AddForce(new Vector3(h,0,v) * speed);
    }
}

控制相机跟随移动

原理:分别计算跟随对象和摄像头的位置,相减得到偏移值offset,之后更新摄像头的位置为跟随对象的位置加上最初的偏移值,则完成跟随

实际应用时可能需要人物移动时相机缓缓的跟过去,而不是始终保持一个相对位置,这个后续讨论

FollowTarget.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowTarget : MonoBehaviour
{
    public Transform targetTransform;
    private Vector3 offset;

    void Start()
    {
        offset = transform.position - targetTransform.position;
    }

    void Update()
    {
        transform.position = targetTransform.position + offset;
    }
}

tansform.position获取组件本身的位置,可以通过直接赋值的方式更新组件的位置

此外还有tansform.Potate表示组件的旋转角度

控制对象旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour
{
    public int x = 1;
    public int y = 0;
    public int z = 0;
    public float speed = 1;
    void Update()
    {
        transform.Rotate(new Vector3(x * speed, y * speed, z * speed));
    }
}
// 遗留问题:怎么将bool型像python一样转化为int类型

通过碰撞检测检测碰撞游戏物体并销毁

怎么知道物体发生碰撞

(1)unity引擎自带碰撞检测事件

OnCollisionEnter(Collision collision) 碰撞发生时触发事件

OnCollisionExit(Collision collision) 碰撞结束时触发事件

OnCollisionStay(Collision collision) 碰撞接触时触发事件

(2)collision 存放碰撞物体的信息

collision.gameObject 跟哪个游戏物体发生碰撞

collision.colloder 跟哪个碰撞器发生碰撞

(3)每一个游戏物体都有一个名字和标签

collision.gameObject.tag获取碰撞的游戏物体的标签

怎么销毁游戏物体

Destroy(gameObject) 销毁游戏物体gameObject的方法

前提条件:挂载对象具有Box collider属性,标签itTag存在,被碰撞物体存在

操作目的:实现碰撞到物体将其摧毁的操作

操作结果:当挂载对象碰撞到指定标签的对象,标签指定的对象摧毁

代码实例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyIt : MonoBehaviour
{
    public string itTag = "ai";
    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag==itTag)
        {
            Destroy(collision.gameObject);
        }
    }
}

触发检测

把Box collider中的is Trigger选项勾上则表示触发检测

触发检测事件

OnTriggerEnter(Collision other) 进入触发区域

OnTriggeExit(Collision other) 离开触发区域

OnTriggeStay(Collision other) 在触发区域待着

控制游戏物体的激活状态

前提条件:游戏物体存在并被获取,state被手动设置

操作目的:设置游戏物体的激活状态

操作结果:可以手动设置被获取物体的激活状态

public gameObject it;
public bool state;
it.SetActive(state);

扩展:查阅unity文档

unity安装时会自带一些命令用法的文档,当遇到需要或者不熟悉的函数时可以通过查阅文档学习

help->unity manual/scripting Reference

扩展:黄金色r=255,g=215,b=0

笔记来源:

01-Unity的介绍和发展_哔哩哔哩_bilibili

猜你喜欢

转载自blog.csdn.net/qq_51943845/article/details/129188545
今日推荐