Unity 向量计算、欧拉角与四元数转换、输出文本、告警、错误、修改时间、定时器、路径、

文章详细介绍了Unity中的C#脚本,涉及向量运算(夹角、模、距离和插值)、欧拉角与四元数转换、时间相关函数(Time.time,Time.timeScale,fixedDeltaTime)以及路径查找和游戏状态检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

public class c2 : MonoBehaviour
{
    // 定时器
    float t1 = 0;

    void Start()
    {
        // 向量
        Vector3 v1 = new Vector3(0, 0, 2);
        Vector3 v2 = new Vector3(0, 0, 3);

        // 计算两个向量的夹角
        Debug.Log(Vector3.Angle(v1, v2));
        // 计算向量的模
        Debug.Log(v2.magnitude);
        // 两点之间的距离 (计算v1、v2 两个点之间的距离) 
        Debug.Log(Vector3.Distance(v1, v2));
        // 计算过程时,参数1 + (参数2 - 参数1)* 参数3
        // 插值 (0,0,0)  (0,0,1)   (0.1f) = 过程:0+ ((1-0)*0.1)  = (0,0,0.1)
        // 插值 (0,0,2)  (0,0,2)   (0.2f) = 过程:2+((2-2)*0.2) = (0,0,0.2)
        // 插值 (0,0,2)  (0,0,2)   (0.2f) = 过程:2+((2-2)*0.2) = (0,0,0.2)
        // 插值 (3,6,1)  (9,5,2)   (0.1f) =
        // 过程:3+ ((9-3)*0.1) = 3.6
        // 过程:6+ ((5-6)*0.1) = 5.9
        // 过程:1+ ((2-1)*0.1) = 1.1
        // 结果:(3.6,5.9,1.1)
        Debug.Log(Vector3.Lerp(new Vector3(3,6,1), new Vector3(9,5,2),0.1f));


        // 欧拉角 x y z
        // 四元数 x y z w

        // 欧拉角
        Vector3 rotate = new Vector3(60, 50, 0);
        // 四元数
        Quaternion quaternion = Quaternion.identity;
        // 欧拉角 转 四元数
        quaternion = Quaternion.Euler(rotate);
        Debug.Log("欧拉角 转 四元数");
        Debug.Log(quaternion);
        // 四元数 转 欧拉角
        Debug.Log("四元数 转 欧拉角");
        Debug.Log(quaternion.eulerAngles);
        // 这是一个朝向敌人的向量
        Vector3 dir = Vector3.left;
        // 获得一个朝向这个向量的旋转
        quaternion = Quaternion.LookRotation(dir);


        // 输出文本
        Debug.Log("输出文本");
        // 输出警告
        Debug.LogWarning("输出警告");
        // 输出错误
        // Debug.LogError("输出错误");

        // 时间相关
        // 游戏开始到现在所用时间
        // Debug.Log(Time.time);

        // 在编辑中-》项目设置-》时间:(时间尺度、固定时间步进)
        // 时间尺度:时间缩放数值
        Debug.Log(Time.timeScale);
        // 修改 时间尺度 (增加重量组件可以看效果)
        // Time.timeScale = 0.1f;

        // 固定时间步进:固定时间间隔
        Debug.Log(Time.fixedDeltaTime);

        // 路径相关
        // 找到 Assets 路径下的 某文件 (可读 某些不可写)若PC端可读可写
        Debug.Log(Application.dataPath + "/test.txt");
        // 持久化路径 可读可写  默认C盘
        Debug.Log(Application.persistentDataPath);
        // 在Asset文件下的 StreamingAssrts 文件夹内的文件 不会被加密(适合放配置文件)
        // 找到 在Asset文件下的 StreamingAssrts路径
        Debug.Log(Application.streamingAssetsPath);
        // 在Asset文件下的 Resources 文件夹内的文件 加载比较方便
        // 找到 临时文件 路径
        Debug.Log(Application.temporaryCachePath);

        // 判断是否 后台运行
        Debug.Log(Application.runInBackground);
        // 打开一个网址
        // Application.OpenURL("http://baidu.com");
        // 退出 (好像有问题,后面再尝试一下)
        // Application.Quit();
    }

    // Update is called once per frame
    void Update()
    {
        // 帧之间的间隔时间(跟硬件相关)
        // Debug.Log(Time.deltaTime);

        // 计时器
        t1 += Time.deltaTime;

        if (t1 >= 5)
        {
            Debug.Log("5秒到了");
            t1 = 0;
        }
    }
}

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

默执_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值