Unity——基础

Mathf

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

public class lesson1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #region Mathf和Math
        //Math是C#中封装好的用于数学计算的工具类——位于system命名空间中
        //Mathf是Unity中封装好的用于数学计算的工具结构体——位于UnityEngine命名空间中

        //他们都是提供来用于进行数学相关计算的
        #endregion

        #region 他们的区别
        //Mathf 和 Math中的相关方法几乎一样
        //Math 是C#自带的工具类 主要就是提供一些数学相关的计算方法
        //Mathf 是Unity专门封装的,不仅包含Math中的方法,还多了一些适用于游戏开发的方法
        //所以我们在进行unity游戏开发时
        //使用Mathf中的方法用于数学计算即可
        #endregion

        #region Mathf中的常用方法——一般计算一次
        //1.Π-PI
        print(Mathf.PI);

        //2.取绝对值-Abs
        print(Mathf.Abs(-10));
        print(Mathf.Abs(-20));
        print(Mathf.Abs(1));

        //3.向上取整-CeilToInt
        float f = 1.3f;
        int i = (int)f;
        print(i);
        print(Mathf.CeilToInt(f));

        //4.向下取整-FloorToInt
        print(Mathf.FloorToInt(9.6F));

        //5.钳制函数-Clamp
        print(Mathf.Clamp(10, 11, 20));//11 比最小还小取最小
        print(Mathf.Clamp(21, 11, 20));//20 比最大还大取最大
        print(Mathf.Clamp(15, 11, 20));//15 在两者之前取本身

        //6.获取最大值-Max
        print(Mathf.Max(1, 2, 3, 4, 5, 6, 7, 8));

        //7.获取最小值-Min
        print(Mathf.Min(1, 2, 3, 4, 5, 6, 7, 8));

        //8.一个数的n次幂-Pow
        print("一个数的n次方" + Mathf.Pow(4, 2));//16
        print("一个数的n次方" + Mathf.Pow(2,3));//8

        //9.四舍五入-RoundToInt
        print("四舍五入" + Mathf.RoundToInt(1.3f));//1
        print("四舍五入" + Mathf.RoundToInt(1.5f));//2

        //10.返回一个数的平方根-Sqrt
        print("返回一个数的平方根"+Mathf.Sqrt(4));//2
        print("返回一个数的平方根"+Mathf.Sqrt(16));//4
        print("返回一个数的平方根"+Mathf.Sqrt(64));//8

        //11.判断一个数是否是2的n次方-IsPowerOfTow
        print("判断一个数是否是2的n次方" + Mathf.IsPowerOfTwo(4));//true
        print("判断一个数是否是2的n次方" + Mathf.IsPowerOfTwo(8));//true
        print("判断一个数是否是2的n次方" + Mathf.IsPowerOfTwo(3));//false

        //12.判断正负数-Sign
        print("判断正负数" + Mathf.Sign(0));//1
        print("判断正负数" + Mathf.Sign(10));//1
        print("判断正负数" + Mathf.Sign(-10));//-1
        #endregion
    }
    float start=0;
    float result=0;
    float time = 0;
    // Update is called once per frame
    void Update()
    {
        #region Mathf中的常用方法——一般不停计算
        //插值运算-Lerp

        //Lerp函数公式
        //result = Mathf。Lerp(start,end,t);

        //t为插值系数,取值范围为0~1
        //result = start + (end-start)*t

        //插值运算用法一
        //每帧改变start的值——变化先快后慢,位置无限接近,但是不会得到end位置
        start = Mathf.Lerp(start, 10, Time.deltaTime);

        //插值运算用法二
        //每帧改变t的值——变化速度匀速,位置每帧接近,当t>=1时,得到结果
        time += Time.deltaTime;
        result = Mathf.Lerp(start, 10, time); 
        #endregion
    }
}

三角函数

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

public class lesson2 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #region 弧度、角度相互转换
        //弧度转角度
        float rad = 1;
        float anger=rad*Mathf.Rad2Deg;
        print(anger);//57.29578

        //角度转弧度
        anger = 1;
        rad = anger * Mathf.Deg2Rad;
        print(rad);//0.01745329
        #endregion

        #region 三角函数
        //注意:Mathf中的三角函数,传入的参数需要时弧度值
        print(Mathf.Sin(30 * Mathf.Deg2Rad));//0.5
        print(Mathf.Cos(60 * Mathf.Deg2Rad));//0.5
        #endregion

        #region 反三角函数
        //注意:反三角函数得到的结果是 正弦或者余弦值对应的弧度
        rad = Mathf.Asin(0.5f);
        print(rad*Mathf.Rad2Deg);//30
        rad = Mathf.Acos(0.5f);
        print(rad * Mathf.Rad2Deg);//60
        #endregion
    }
}

坐标系

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

public class lesson3 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #region 世界坐标系
        //目前学习的和世界坐标系相关的
        //this.transform.position;
        //this.transform.rotation;
        //this.transform.eulerAngles;
        //this.transform.lossyScale;
        #endregion

        #region 物体坐标系
        //相对父对象的物体坐标系的位置 本地坐标 相对坐标
        //this.transform.localPosition;
        //this.transform.localRotation;
        //this.transform.localEulerAngles;
        //this.transform.localScale;
        //修改他们 会是相对父对象物体坐标系的变化
        #endregion

        #region 屏幕坐标系
        //Input.mousePosition;
        //Screen.width;
        //Screen.height;
        #endregion

        #region 视口坐标系
        //摄像机上的 视口范围
        #endregion

        #region 坐标转换相关 
        //世界转本地
        //this.transform.InverseTransformDirection;
        //this.transform.InverseTransformPoint;
        //this.transform.InverseTransformVector;

        //本地转世界
        //this.transform.TransformDirection;
        //this.transform.TransformPoint;
        //this.transform.TransformVector;

        //世界转屏幕
        //Camera.main.WorldToScreenPoint
        //屏幕转世界
        //Camera.main.ScreenToWorldPoint
        
        //世界转视口
        //Camera.main.WorldToViewportPoint
        //视口转世界
        //Camera.main.ViewportToWorldPoint

        //视口转屏幕
        //Camera.main.ViewportToScreenPoint
        //屏幕转视口
        //Camera.main.ScreenToViewportPoint
        #endregion
    }
}

向量模长和单位向量

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

public class lesson4 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #region 向量
        //三维向量-Vector3
        //Vector3有两种几何意义
        //1.位置——代表一个点
        print(this.transform.position);

        //2.方向——代表一个方向
        print(this.transform.forward);
        print(this.transform.up);
        Vector3 v3 = new Vector3(1, 2, 3);
        Vector2 v2 = new Vector2(1, 2);
        #endregion

        #region 两点决定一向量
        //A和B此时 几何意义是两个点
        Vector3 A = new Vector3(1, 2, 3);
        Vector3 B = new Vector3(5, 1, 5);
        //求向量
        //此时 AB和BA代表的 是两个向量
        Vector3 AB = B - A;
        Vector3 BA = A - B;
        #endregion

        #region 零向量和负向量
        print(Vector3.zero);

        print(Vector3.forward);
        print(-Vector3.forward);
        #endregion

        #region 向量的模长
        //Vector3中提供了获取向量模长的成员属性
        print(AB.magnitude);
        Vector3 C = new Vector3(5, 6, 7);
        print(C.magnitude);

        //Vector3.Distance(A, B);
        #endregion

        #region 单位向量
        //Vector3中提供了获取单位向量的成员属性
        //normalized
        print(AB.normalized);
        print(AB/AB.magnitude);
        #endregion
    }
    //总结
    //1.Vector3 可以表示一个点 也可以表示一个向量 具体表示什么 是根据我们的具体需求和逻辑决定
    //2.如何在Unity里面得到向量 终点减起点 就可以得到向量 点也可以代表向量 代表的就是oc向量 o是坐标系原点
    //3.得到了向量 就可以利用Vector3中提供的成员属性 得到模长和单位向量
    //4.模长相当于可以得到两点之间的距离 单位向量 主要是用来进行移动计算的 它不会影响我们想要的移动效果
}

向量加减乘除

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

public class lesson5 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #region 向量加法
        //this.transform.position+=new Vector3 (1,2,3);
        this.transform.Translate(Vector3.forward * 5);
        #endregion

        #region 向量减法
        //this.transform.position-=new Vector3 (1,2,3);
        this.transform.Translate(-Vector3.forward * 5);
        #endregion

        #region 向量乘除标量
        this.transform.localScale *= 2;
        this.transform.localScale /= 2;
        #endregion
    }
}

向量点乘

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

public class lesson6 : MonoBehaviour
{
    public Transform target;
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        #region 调试画线
        //在场景窗口显示,游戏窗口不显示
        //画线段
        //前两个参数 分别是 起点 终点
        //Debug.DrawLine(this.transform.position, this.transform.position+this.transform.forward, Color.red);
        
        //画射线
        //前两个参数分别是 起点 方向
        //Debug.DrawRay(this.transform.position, this.transform.forward, Color.white);
        #endregion

        #region 通过点乘判断对象方位
        //Vector3 提供了计算点乘的方法
        Debug.DrawRay(this.transform.position, this.transform.forward, Color.red);
        Debug.DrawRay(this.transform.position, target.position-this.transform.position, Color.red);
        //得到两个向量的点乘结果
        //向量 a 点乘 AB 的结果
        float dotResult = Vector3.Dot(this.transform.forward, target.position - this.transform.position);
        if (dotResult >= 0)
            print("它在我前方");
        else
            print("它在我后方");
        #endregion

        #region 通过点乘推导公式算出夹角
        //步骤
        //1.用单位向量算出点乘结果
        dotResult = Vector3.Dot(this.transform.forward, target.position - this.transform.position);
        //2.用反三角函数得出角度
        print("角度" + Mathf.Acos(dotResult)*Mathf.Rad2Deg);

        //Vector3中提供了 得到两个向量之间夹角的方法
        print("角度2" + Vector3.Angle(this.transform.forward, target.position - this.transform.position));
        #endregion
    }
}

向量叉乘

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

public class lesson7 : MonoBehaviour
{
    public Transform A;
    public Transform B;

    // Start is called before the first frame update
    void Start()
    {
        #region 向量叉乘计算
        print(Vector3.Cross(A.position, B.position));
        #endregion

        #region 向量叉乘几何意义
        //假设向量A和B都在XZ平面上
        //向量A叉乘向量B
        //y大于0 证明 B在A右侧
        //y小于0 证明 B在A左侧
        #endregion
    }

    // Update is called once per frame
    void Update()
    {
        //Vector3 C = Vector3.Cross(A.position, B.position);
        //if (C.y > 0)
        //    print("B在A的右侧");
        //else
        //    print("B在A的左侧");

        Vector3 C = Vector3.Cross(B.position, A.position);
        if (C.y > 0)
            print("A在B的右侧");
        else
            print("A在B的左侧");
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity3D格斗游戏源码是一种让开发者能够开发自己的格斗游戏的一种资源,而“仿最终幻想”是模仿最终幻想系列游戏来设计和开发的游戏。这种源码提供了许多基本的游戏元素和功能,开发者可以根据自己的需求来创建自己想要的游戏。 在Unity3D格斗游戏源码中,主要包含了以下几个方面的内容: 1. 角色控制:开发者可以通过源码来实现角色的移动、攻击、防御等基本动作。游戏中的角色可以使用键盘、鼠标或者手柄进行操控,使得玩家能够与游戏世界进行交互。 2. 动画系统:为了增强游戏的流畅性和真实感,该源码还提供了动画系统。开发者可以根据需要创建角色的各种动画,例如攻击动画、受伤动画和死亡动画等,使得游戏体验更加逼真。 3. AI系统:为了让游戏增加一定的挑战性,该源码还提供了AI系统。开发者可以通过代码设置敌方角色的行为和策略,使得游戏中的敌人具有一定的智能和反应能力。 4. 特效和音效:为了提升游戏的视听效果,该源码还包括了一些特效和音效资源。开发者可以根据自己的需要添加各种特效和音效,增强游戏的氛围和乐趣。 5. 可定制性:该源码还提供了一些可配置的参数和选项,开发者可以根据自己的需求来调整游戏的各种设置,包括角色属性、技能系统和游戏难度等,以便创造出不同的游戏体验。 总之,Unity3D格斗游戏源码可以帮助开发者快速搭建一个仿照最终幻想系列的格斗游戏。通过使用该源码,开发者可以省下许多开发时间和精力,同时也能够在这个基础上进行二次开发,实现自己的创意和想法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值