两点直线补间

/// <summary>
/// 两点之间直线的坐标计算.
/// </summary>
/// <returns>The line paht help.</returns>
/// <param name="startPoint">起点.</param>
/// <param name="endPoint">终点.</param>
/// <param name="per">从起点到终点每间隔几米划分一个点.</param>
public static List<Vector3> ToLinePahtHelp(Vector3 startPoint, Vector3 endPoint, float per = 0.27f)
{List<Vector3> pointList = new List<Vector3>(777);Vector3 dir = (endPoint - startPoint).normalized;Vector3 currentPoint = Vector3.zero; ;float distance = Vector3.Distance(endPoint, startPoint);int number = (int)(distance / per);for (int i = 1; i < number + 1; i++){currentPoint = startPoint + dir * (float)(i * per);pointList.Add(currentPoint);}return pointList;
}

多点之间曲线补间

using System;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;public class Test_05 : MonoBehaviour
{Transform a, b,c,d;// Start is called before the first frame updatevoid Start(){a =GameObject.Find("Cube_A").transform;b = GameObject.Find("Cube_B").transform;c = GameObject.Find("Cube_C").transform;d = GameObject.Find("Cube_D").transform;}// Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.Mouse0)){var pointList = new List<Transform>(777);pointList.Add(a);pointList.Add(b);DebugDrawPath(CaulaterPath(pointList.Select(x => x.position).ToArray()), Color.red, 10f);}if (Input.GetKeyDown(KeyCode.Mouse1)){var pointList = new List<Transform>(777);pointList.Add(a);pointList.Add(b);pointList.Add(c);pointList.Add(d);DebugDrawPath(CaulaterPath(pointList.Select(x=>x.position).ToArray()), Color.yellow, 10f);}}/// <summary>/// 多点之间曲线补间,计算出这些点曲线间隔/// </summary>/// <param name="path"></param>/// <returns></returns>public static Vector3[] CaulaterPath(Vector3[] path){Vector3[] vector3s = PathControlPointGenerator(path);int SmoothAmount = path.Length * 20;var points = new Vector3[SmoothAmount+1];points[0] = Interp(vector3s, 0);for (int i = 1; i <= SmoothAmount; i++){float pm = (float)i / SmoothAmount;Vector3 currPt = Interp(vector3s, pm);points[i]=currPt;}return points;}/// <summary>/// 绘制多点线段/// </summary>/// <param name="path"></param>/// <param name="color"></param>/// <param name="duration"></param>public void DebugDrawPath(Vector3[] path, Color color = default(Color), float duration = 0.1f){var length = path.Length;for (int i = 0; i < length; i++){var curr = path[i];//最后一个点if (i == length - 1){var forwardPos = path[i - 1];Debug.DrawLine(curr, forwardPos, color, duration);}else{var nextPos = path[i + 1];Debug.DrawLine(curr, nextPos, color, duration);}}}private static Vector3[] PathControlPointGenerator(Vector3[] path){Vector3[] suppliedPath;Vector3[] vector3s;//create and store path points:suppliedPath = path;//populate calculate path;int offset = 2;vector3s = new Vector3[suppliedPath.Length + offset];Array.Copy(suppliedPath, 0, vector3s, 1, suppliedPath.Length);vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]);vector3s[vector3s.Length - 1] = vector3s[vector3s.Length - 2] + (vector3s[vector3s.Length - 2] - vector3s[vector3s.Length - 3]);//is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!if (vector3s[1] == vector3s[vector3s.Length - 2]){Vector3[] tmpLoopSpline = new Vector3[vector3s.Length];Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);tmpLoopSpline[0] = tmpLoopSpline[tmpLoopSpline.Length - 3];tmpLoopSpline[tmpLoopSpline.Length - 1] = tmpLoopSpline[2];vector3s = new Vector3[tmpLoopSpline.Length];Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);}return vector3s;}private static Vector3 Interp(Vector3[] pts, float t){int numSections = pts.Length - 3;int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - 1);float u = t * (float)numSections - (float)currPt;Vector3 a = pts[currPt];Vector3 b = pts[currPt + 1];Vector3 c = pts[currPt + 2];Vector3 d = pts[currPt + 3];return .5f * ((-a + 3f * b - 3f * c + d) * (u * u * u)+ (2f * a - 5f * b + 4f * c - d) * (u * u)+ (-a + c) * u+ 2f * b);}
}

Unity3d Vector3点相关推荐

  1. Unity3d vector3.forward和transform.forward的区别!

    在unity3d中有2个forward,一个是vector3.forward和transform.forward,这两个forward其实完全不一样.他们之间的区别主要体现在在不同坐标系时的反映上. ...

  2. unity3d Vector3.Lerp解析

    Vector3.Lerp:http://www.ceeger.com/Script/Vector3/Vector3.Lerp.html 手册中描述的不是很详细,什么叫"按照数字t在from到 ...

  3. 时光煮雨 Unity3D实现2D人物移动-总结篇

    系列目录 [Unity3D基础]让物体动起来①--基于UGUI的鼠标点击移动 [Unity3D基础]让物体动起来②--UGUI鼠标点击逐帧移动 时光煮雨 Unity3D让物体动起来③-UGUI DoT ...

  4. 千锋Unity学习笔记

    学习笔记:[千锋合集]史上最全Unity3D全套入门教程|匠心之作 文章目录 初级 1.0数学 1.0点乘叉乘 1.点乘: 2.叉乘: 2.0Mathf 3.0Vector 4.0旋转 2.0组件 3 ...

  5. Unity3D基础API之Vector3

    Vector3 静态方法 1.Vector3.Angle()--两个向量之间的夹角 public static float Angle(Vector3 from, Vector3 to) 单位:度: ...

  6. unity3d问题集 对Vector3.Lerp 插值的理解 - 转自蛮牛

    有时,我们在做游戏时会发现有些跟随动作不够圆滑或者需要一个缓冲的效果,这时,一般会考虑到插值.所以对插值的理解是必需的.(比如摄像机跟随主角) 插值是数学上的一个概念,在这里用公式表示就是:from  ...

  7. unity3d教程运行物理机制

    首先,我们将把Hooke定律写Euler方法结合在一起找到新坐标.加速和速度. Hooke定律是F=kx,这里的F是指由水流产生的力(记住,我们将把水体表面模拟为水流),k是指水流的常量.x则是位移. ...

  8. 【跟我一起学Unity3D】做一个2D的90坦克大战之AI系统

    对于AI,我的初始想法非常easy,首先他要能动,而且是在地图里面动. 懂得撞墙后转弯,然后懂得射击,其它的没有了,基于这个想法,我首先创建了一个MyTank类,用于管理玩家的坦克的活动,然后创建AI ...

  9. Unity3D常用代码总结

    1 GUI汇总 function OnGUI() { GUI.Label(Rect(1,1,100,20),"I'm a Label"); //1 GUI.Box(Rect(1,2 ...

最新文章

  1. GitHub使用方法
  2. JZOJ5906 传送门
  3. (码友推荐)2018-07-12 .NET及相关开发资讯速递
  4. 函数指针与回调函数、句柄
  5. 电缆沟巡查机器人_电缆沟道巡检机器人的制作方法
  6. HDU 5978 2016ICPC大连 H: To begin or not to begin
  7. [转载] python 把几个DataFrame合并成一个DataFrame——merge,append,join,conca
  8. 【武忠祥高等数学基础课笔记】第一章 函数、极限、连续
  9. MT7621芯片技术资料分析,MT7621数据表原理图
  10. pytorch crnn 笔记(二)
  11. win7共享中心服务器运行失败,Win7系统网络与共享中心无法打开怎样解决?
  12. 《青山翠影》玖 独行的时代 | 去程归程
  13. 架构师进阶之路——1、持久化框架(一)
  14. Java(老白再次入门) - 异常处理
  15. C++11 Type-rich编程
  16. 数据采集-呼吸心跳信号检测方法(二)
  17. java 下载db文件_Java下载文件自定义名称和格式类型
  18. Qt开发 — 显示gif动画
  19. 模板 - 稳定婚姻问题
  20. 富文本编辑:wangEditor使用教程

热门文章

  1. 《伏C录》神兵百解篇-重铸struct关键字之心
  2. 一个程序员的世界观(一):从一个玉坠说起
  3. HTML+CSS——实现跳动的心
  4. 2018年10月18日提高组 T3 摘果子
  5. 爬虫之urllib2库的自定义Opener
  6. 平面设计学习路线及其书籍推荐
  7. 修正牛顿法matlab,牛顿算法及其改进【阻尼牛顿法、修正牛顿法】
  8. 深度总结!详细复盘我的两轮携程+映客面经,看完对你的面试也很有帮助。
  9. 【PaddleNLP-kie】关键信息抽取2:UIE模型做图片信息提取全流程
  10. 关于苹果内购问题游客登录的解决过程