zoukankan      html  css  js  c++  java
  • 获得两点之间连续坐标,向量加法、减法、乘法的运用

    这是一个很小的知识点,不过我觉得可以记录下来,就算是在大都项目都是这些小小的知识点堆积而来,之前我用这个小知识点获取了,两点之间都一系列点,今天当我想做一个移动的时候,发现这个知识点也能做这方面都事情,所以我觉得把它记录下来,虽然很简单,但是用处并不简单。

    using UnityEngine;
    using System.Collections;
    
    /// <summary>
    /// 下面这个类是将一个transfrom对象移动到目标点(100,0,100)位置
    /// 主要的方法GetBetweenPoint,能够获取起始点和终点之间都由起始点朝向终点都distance距离点
    /// </summary>
    public class Main : MonoBehaviour 
    {
        private Vector3 target = new Vector3(100, 0, 100);
        private int count = 0;
        // Use this for initialization
        void Start ()
        {
    
        }
        
        // Update is called once per frame
        void Update ()
        {
            if (Vector3.Distance(transform.position, target) > 0.1f)
            {
                transform.position = GetBetweenPoint(transform.position, target, 0.1f);
            }
            else
            {
                transform.position = target;
            }
        }
    
        /// <summary>
        /// 获取两点之间的一个点,在方法中进行了向量的减法,以及乘法运算
        /// </summary>
        /// <param name="start">起始点</param>
        /// <param name="end">结束点</param>
        /// <param name="distance">距离</param>
        /// <returns></returns>
        private Vector3 GetBetweenPoint(Vector3 start, Vector3 end, float distance)
        {
            Vector3 normal = (end - start).normalized;
            return normal * distance + start;
        }
    }
    using UnityEngine;
    using System.Collections;
     
    /// <summary>
    /// 这个类和上面都类功能是相同都,都是从当前坐标移动到目标坐标点,只是向量的计算方式不一样,这种方式是向量的加法运算
    /// </summary>
    public class Main : MonoBehaviour 
    {
        private Vector3 target = new Vector3(100,0,100);
        private Vector3 normal;
        // Use this for initialization
        void Start () 
        {
        //向量的减法运算
            normal = (target - transform.position).normalized;
        }
        
        // Update is called once per frame
        void Update ()
        {
            if (Vector3.Distance(transform.position, target) > 0.1)
            {
                //向量的加法运算
                transform.position = transform.position + normal * 0.1f;
            }
            else
            {
                transform.position = target;
            }
        }
    }

     来源:http://blog.gamerisker.com/archives/496.html

  • 相关阅读:
    [USACO Mar08] 牛跑步 --k短路
    [ZJOI2008]树的统计Count
    [SDOI2010]魔法猪学院 --k短路
    POJ 2449 Remmarguts' Date -K短路
    [SCOI2007]kshort--k短路
    [HAOI2015]树上操作 -树链剖分
    HDU Aragorn's Story -树链剖分
    [USACO09JAN]安全出行Safe Travel
    2019全球区块链杭州高峰论坛将于5月17日举办!
    2019亚洲物联网安全创新国际峰会将于5月在上海开幕!
  • 原文地址:https://www.cnblogs.com/dj1232090/p/9859078.html
Copyright © 2011-2022 走看看