返回首页DA系统C#IDE文件同步服务屏保 今天是: 2026-05-05    "立夏"  夏季的第一个节气,表示盛夏时节的正式开始

搜索
热搜: linux 技术
Hi~登录注册
查看: 1970|回复: 0

[原创] 【原创】Vector类讲解

[复制链接]
发表于 2021-8-4 20:33:37 | 显示全部楼层 |阅读模式
本帖最后由 da11 于 2021-8-4 20:37 编辑

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

/// <summary>
/// 讲解Vector类的脚本
/// </summary>
public class VectorDemo : MonoBehaviour
{
    public Transform t1;

    private Vector3 planeNorm;

    private Vector3 tagTF;

    private float x;

    private float timeX;

    public AnimationCurve animCurve;

    private void Start()
    {
        tagTF = new Vector3(0, 0, 10);
        timeX = 2;
    }

    private void Update()
    {
        Vector3 vect = new Vector3(0, 0, 10);

        planeNorm = new Vector3(0, 1, 0);

        //将归一化的(0,0,1)赋值给norm
        //Vector3 norm = vect.normalized;

        //将归一化的(0,0,1)数值直接修改为vect的新参数
        //Vector3.Normalize(vect);  

        //获取物体投影的向量方法:ProjectOnPlane
        //第一个参数是被投影物体位置,第二个位置是投影的面


        Vector3 TestVector3 = Vector3.ProjectOnPlane(t1.position, planeNorm);

        Debug.DrawLine(Vector3.zero, TestVector3,Color.red);
        Debug.DrawLine(Vector3.zero, t1.position);

        //获取物体随法线反射的方法
        //第一个参数是参照向量,第二个位置是反射的法线位置



        //Vector3 TestVector3 = Vector3.Reflect(t1.position, planeNorm);

        //Debug.DrawLine(Vector3.zero, TestVector3,Color.red);
        //Debug.DrawLine(Vector3.zero, t1.position);
    }

    private void OnGUI()
    {
        if (GUILayout.RepeatButton("                                              回到原点"))
        {
            this.transform.position = new Vector3(0,0,0);
            x = 0;
        }

        if (GUILayout.RepeatButton("                                              快速移动"))
        {
            this.transform.position = tagTF;
        }

        if (GUILayout.RepeatButton("                                              缓慢移动"))
        {
            //按速度移动的方法:MoveTowards
            //第一个参数,开始位置,第二个参数,终点位置,第三个参数,移动速度
            this.transform.position = Vector3.MoveTowards(this.transform.position, tagTF, 10 * Time.deltaTime);
        }

        if (GUILayout.RepeatButton("                                              变速移动-终点与速度比例固定"))
        {
            //变速移动的方法:MoveTowards
            //第一个参数,开始位置,第二个参数,终点位置,第三个参数,变速速度
            //特点,先快后慢移动,逐渐接近终点,但是永远不会到达终点
            //终点与速度比例固定,现象:先快后慢
            this.transform.position = Vector3.Lerp(this.transform.position, tagTF, 1 * Time.deltaTime);
        }


        if (GUILayout.RepeatButton("                                              变速移动-起点与终点固定"))
        {
            //没调用一次方法就加一个时间间隔
            //实际原理:多长时间走到终点的值,如果除号前面是x + Time.deltaTime,除号后面到终点的秒数
            x = x + Time.deltaTime / timeX;  

            //变速移动的方法:MoveTowards
            //第一个参数,开始位置,第二个参数,终点位置,第三个参数,变速速度
            //起点与终点固定,变速速度使用Curve方法实现变速
            //通过调用AnimationCurve实例的Evaluate方法,实参给x(速度变化值),则返回一个y值(float),实现现象需要通过public AnimationCurve animCurve设置速度曲线来恒定
            //注意,Lerp方法认为y的值如果超过1,就认为还是1。如需使用超过1的y值,请使用LerpUnclamped方法
            //Lerp是强制固定终点,y值怎么高于1,也还会等于1,LerpUnclamped是不强制固定终点,y值的值是多少即移动多少

            //this.transform.position = Vector3.Lerp(Vector3.zero, tagTF, animCurve.Evaluate(x));
            this.transform.position = Vector3.LerpUnclamped(Vector3.zero, tagTF, animCurve.Evaluate(x));
        }
    }
}



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册成为修仙之旅的少年~

x
游客
回复
*滑块验证:

DA论坛飞机票来了~
快速回复 返回顶部 返回列表