位置: 编程技术 - 正文

Unity API 笔记(unity常用api)

编辑:rootadmin

推荐整理分享Unity API 笔记(unity常用api),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:unity bip,unity官方api,unity bip,unity常用api,unity bip,unity bip,unity.api,unity api中文手册,内容如对您有帮助,希望把文章链接给更多的朋友!

#1 Rotate Translate函数只是一次改变旋转和位置 是因为放在了Update函数里才使其有了平滑的连续的动画效果

如果把Rotate函数放在Start()函数里 旋转&#;会立刻改变 没有过渡。

#2

插&#;类函数 (Interpolating Functions)

Mathf.Lerp(a,b,t)

当t<0 时 t按0计算 当t>1时 t按1计算 当t属于[0,1]时 函数的返回&#;为a&#;(b-a)*t 连续的Update调用使其产生平滑过渡的效果。

类&#;的还有Vector3.Lerp Color.Lerp Quaternion.Lerp

有个类&#;但又不同的函数 Mathf.MoveTowards

static float MoveTowards(float current, float target, float maxDelta);

是以maxDelta为增量进行&#;的改变 当maxDelta为负时 current将远离target

static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);

按直线把一个点从current移动到target 返回&#;是从current到target直线上离current距离为maxDistanceDelta的点的坐标

如果两点的连线距离不足maxDistanceDelta,则直接到达target 也就是说返回&#;永远不会超过target. 如果是maxDistanceDelta是负&#; 则current点将按直线远离target.

如print(Vector3.MoveTowards(new Vector3(0,0,0),new Vector3(3,4,0),1)); 结果是 (0.6,0.8,0.0)

同样有个类&#;的函数Mathf.MoveTowardsAngle

static float MoveTowardsAngle(float current, float target, float maxDelta);

和MoveToward类&#; 但是确保当超过度时按照角度的方式计算 由于优化原因 不支持负的maxDelta

current和target代表的角都是以角度为单位的、所以要注意:旋转角度之间的插&#;应该用MoveTowardsAngle 类&#;的函数还有Mathf.LerpAngle

球形插&#;Vector3.Slerp与线性插&#;Lerp不同之处在于将传入的向量当做方向处理 而非原来当做点来处理 且它的模也会被插&#;处理。

反过来计算需要多少插&#;参数才能从current到target的函数 Mathf.InverseLerp(from,to,value)

如 print(Mathf.InverseLerp(5,,8)); 结果为(8-5)/(-5)=0.6

Summary:

Lerp类函数:

Mathf.Lerp()

Vector3.Lerp()

Color.Lerp()

Quaternion.Lerp()

对应的角度插&#;函数为

Mathf.LerpAngle()

Slerp类函数

Vector3.Slerp()

MoveTowards类函数

Mathf.MoveTowards()

Vector3.MoveTowards()

对应的角度插&#;函数为

Mathf.MoveTowardsAngle()

反插&#;函数

Mathf.InverseLerp()

夹&#;函数

Mathf.Clamp

static float Clamp(float value, float min, float max);

static int Clamp(int value, int min, int max);

将value夹在min和max之间 如果value < min 则 返回min 如果value > max 则返回max 不改变value的&#;

static float Mathf.PingPong(float t,float length)

返回&#;将在[0,length]之间有序来回震荡 注意要放在Update()函数里面

让物体的x坐标在[0,3]来回震荡。

Transform.Translate()

重载1:

void Translate(Vector3 translation, Space relativeTo = Space.Self);

移动位移translation 默认是自身坐标系

相对:

coordinate(坐标)

Space.Self 自身坐标系

Space.World 世界坐标系

重载2:

void Translate(float x, float y, float z, Space relativeTo = Space.Self);

沿x轴位移x 沿y轴位移y 沿z轴位移z

重载3:

void Translate(Vector3 translation, Transform relativeTo);

位移向量translation 以relativeTo的局部坐标为参照 如果relativeTo为null 则以世界坐标为参照系。

重载4:

void Translate(float x, float y, float z, Transform relativeTo);

Transform.Rotate()

重载1:void Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self);

按照欧拉角旋转物体(z,x,y) 注意顺序(旋转顺序不同最终旋转效果也可能不同) 单位是角度

类&#;于Translate 默认是物体自身的局部坐标系

注意:以世界坐标旋转并不是绕着世界坐标轴旋转 而只是按照世界坐标系的方向 以自身枢轴点旋转。

重载2:void Rotate(float xAngle, float yAngle, float zAngle, SpacerelativeTo = Space.Self);

绕z轴z度 绕x轴x度 绕y轴y度 注意顺序

重载3:void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self);

绕axis向量旋转angle度 默认axis向量的坐标系是物体自身的局部坐标系

Unity中采用左手坐标系

Vector3类

普通成员变量:

x y z 分量

this[index] (0:x 1:y 2:z)

eg:

只读:

magnitude 模

sqrMagnitude 模的平方(效率)

normalized 标准化向量

静态成员变量:

zero (0,0,0)

one (1,1,1)

缩写(shorthand)静态数据成员不用new

static Vector3 right;:Shorthand for writing Vector3(1, 0, 0). x轴

static Vector3 up; : Shorthand for writing Vector3(0, 1, 0).y轴

static Vector3 forward;:Shorthand for writing Vector3(0, 0, 1). z轴

对应的有left(-1,0,0)down(0,-1,0)back(0,0,-1)

构造函数

重载1 Vector3(float x,float y ,float z)

重载2 Vector3(float x,float y)

sets z to zero

普通成员函数:

void Set(float new_x, float new_y, float new_z);

重新为一个已存在的向量设置x y z分量

string ToString();string ToString(string format);Unity API 笔记(unity常用api)

静态函数:

static float Angle(Vector3 from, Vector3 to); 返回from和to的夹角 (单位为度 不大于度)

static Vector3 ClampMagnitude(Vector3 vector, float maxLength); 返回一个向量的拷贝 其模长不超过maxLength

static Vector3 Cross(Vector3 lhs, Vector3 rhs); 叉乘

static float Distance(Vector3 a, Vector3 b); 返回 a b 两点的距离 与(a-b).magnitude 作用相同

static float Dot(Vector3 lhs, Vector3 rhs); 点乘

static Vector3 Lerp(Vector3 from, Vector3 to, float t); 线性插&#;

static Vector3 Max(Vector3 lhs, Vector3 rhs); 返回模较大的向量

static Vector3 Min(Vector3 lhs, Vector3 rhs); 返回模较小的向量

static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta); 线性步移向量void Normalize(); 规范化(改变原始向量) 如 Vector3 p = Vector3.one; p.Normalize(); 如果不想改变原始向量 可以使用normalized数据成员或者Vector3.Normalize(p) 函数如果向量太小如(1e-8,0,0) 它将会被置为零向量static void OrthoNormalize(Vector3 normal, Vector3 tangent); 规范正交化两个向量static void OrthoNormalize(Vector3 normal, Vector3 tangent,Vector3 binormal); normal 是 tangent 和binormal的法线 tangent和binormal也规范正交 即 规范正交化三个向量 (在转换坐标系进行模型缩放时有用)static Vector3 Project(Vector3 vector, Vector3 onNormal); 返回vector在OnNormal上的投影向量 static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal); 返回以inNormal为法线的平面上 inDirection的反射向量 (模相等)static Vector3 RotateTowards(Vector3 current, Vector3 target, floatmaxRadiansDelta, float maxMagnitudeDelta);以maxRadiansDelta(单位是弧度)旋转 以maxMagnitudeDelta改变模长 将current 逐渐变到 target 这里是将current和target当做向量而非点如 旋转到目标位置

static Vector3 Scale(Vector3 a, Vector3 b);

a和b的各分量对应相乘得到一个新的向量

输出 (2.0,6.0,.0) 用于各个方向的缩放操作

static Vector3 Slerp(Vector3 from, Vector3 to, float t);

模拟日出日落效果

static Vector3 SmoothDamp(Vector3 current, Vector3 target, Vector3 currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, floatdeltaTime = Time.deltaTime);

弹簧效果的阻&#; 先快后慢 速度是ref

smoothtime 从current到达target大约需要耗费的时间

deltaTime 上一次函数调用至此次调用间隔的时间间隔

The most common use is for smoothing a follow camera.

运算:

- :向量减法 反向量

!=:Very close vectors are treated as being equal.

*:向量的数乘 注意只定义了Vector3与float的乘法 double没有

/:static Vector3 operator /(Vector3 a, float d); 向量的数除

&#;:向量的加法 对应分量相加

==:This will also return true for vectors that are really close to being equal.

由此可见 Unity中的Vector3的==已经考虑了浮点比较误差 因此我们不需要像C语言那样通过epsilon的约束来比较两个浮点数

Quaternion类

用来表示旋转 没有欧拉角的Gimbal Lock(万向节死锁)问题 易插&#;计算 Unity内部旋转处理方式

四元数:

1概念 Q=[w,(x,y,z)] 一个四元数包含一个标量分量和一个三维向量分量

在3D数学中用单位四元数来表示旋转 对于旋转轴为n=(nx,ny,nz) 旋转角度为α的旋转 用四元数表示:

w=cos(α/2)

x=sin(α/2)Nx

y=sin(α/2)Ny

z=sin(α/2)Nz

简记为 Q=[cos(α/2),sin(α/2)n]

一般为了计算方便 要求n为单位矢量

静态成员变量:

identity:无旋转 (Read Only) 实验得&#;为(0,0,0,1) (x,y,z,w)

普通成员变量:

Vector3 eulerAngles 返回对应的欧拉角 旋转顺序为 z x y 即物体在Inspector显示的Rotation的转换成--范围后的结果

this[int] :获取分量 x y z w 对应 0 1 2 3

x y z w :float

构造函数

Quaternion(x,y,z,w)

成员函数

void Set(float new_x, float new_y, float new_z, float new_w);

为一个已经存在的四元数设置x,y,z,w;

void SetFromToRotation(Vector3 fromDirection, Vector3toDirection);

将当前四元数设置为一个从from旋转到to方向的旋转

如:

public Quaternion rotation = Quaternion.identity; void Start() { rotation.SetFromToRotation(new Vector3(0, 1, 0), new Vector3(0, 0, 1)); print(rotation.eulerAngles); }

应该绕x轴旋转度 所以输出(.0,0.0,0.0) 旋转轴是(1,0,0) 所以rotation被设置成(0.(cos(/2)),0,0,0.) 符合前面四元数的概念定义。

void SetLookRotation(Vector3 view, Vector3 up = Vector3.up);

设置朝向一个位置的旋转 即令物体的z轴对准view物体 x轴与up垂直 这个函数一般用于令物体面对另一物体

是 零向量 将抛出一个提示信息

up可以是零向量 表示对x轴的朝向没有要求

void ToAngleAxis(float angle, Vector3 axis);

把一个四元数表示的旋转转换成角-轴表示的形式

out参数 注意是ToAngleAxis 而非ToAxisAngle 后者已经过时

string ToString();string ToString(string format);

静态成员函数

static float Angle(Quaternion a, Quaternion b);

返回两个旋转z轴的夹角 单位是度

print(Quaternion.Angle(new Quaternion(0, 0, 0, 1), new Quaternion(0,Mathf.Sqrt(2)/2,0,Mathf.Sqrt(2)/2))); 输出为

static Quaternion AngleAxis(float angle, Vector3 axis);

返回一个绕axis轴旋转angle度的等价的Quaternion旋转

如 print(Quaternion.AngleAxis(, Vector3.up)); 输出(0.0,0.7,0.0,0.7)

static float Dot(Quaternion a, Quaternion b);

static Quaternion Euler(float x, float y, float z);

static Quaternion Euler(Vector3 euler);

由一个绕z轴z度 x轴x度 y轴y度得到的旋转转换成Quaternion

static Quaternion FromToRotation(Vector3 fromDirection, Vector3toDirection);

类&#;SetFromToRotation

static Quaternion Inverse(Quaternion rotation);

返回一个旋转的逆

static Quaternion Lerp(Quaternion from, Quaternion to, float t); 从from到to之间用t插&#;然后将结果标准化,该函数比Slerp处理速度更快 但是当from和to相隔较远时效果较差。

static Quaternion LookRotation(Vector3 forward, Vector3 upwards= Vector3.up);

类&#;SetLookRotation函数

static Quaternion RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta);

将旋转from旋转到to

通过一个角位移maxDegreesDelta来进行旋转 注意不会越界 单位是角度

如果角位移是负的 那么from将远离to 直到to的反向为止(为止的意思是停止运动 实验得到会在附近有轻微的震荡)

static Quaternion Slerp(Quaternion from, Quaternion to, float t);

球形差&#;

eg

运算:

!= :该函数通过判断两个Quaternion的Dot Product是否小于1.0来运作。

注意:因为quaternion能够表示两圈内的旋转() 所以即使两者的旋转效果看起来不一样 比较结果也可能返回true.

*:结合两个旋转 a*b 效果是 先旋转a 后旋转b (!= b * a)不满足交换律 类&#;于矩阵的乘法效果

==:注意:因为quaternion能够表示两圈内的旋转() 所以即使两者的旋转效果看起来一样 比较结果也可能返回false 比如旋转 度和旋转度

cos5° 不等于 cos° Dot == 1.0 则两者相等

.

unity学习之NGUI做NPC对话 首先今天我主要做了一下,用NGUI做的NPC对话,效果有点不好看,就凑合看吧。我首先用NGUI打了一个界面,然后为sprite添加了button事件,然后接下来就是

UGUI 加载图片 图片是动态加载的,然后转换为sprite赋到ugui的按钮上该文章出自【狗刨学习网】代码如下:usingUnityEngine;usingSystem.Collections;usingSystem.IO;usingUnityEngine.UI;pub

Unity3D深入浅出 - Shader基础开发 Mecanim概述:Mecanim是Unity提供第一个丰富而复杂的动画系统,提供了:针对人形角色的简易的工作流和动画创建能力Retargeting(运动重定向)功能,即把动画

标签: unity常用api

本文链接地址:https://www.jiuchutong.com/biancheng/380213.html 转载请保留说明!

上一篇:Unity3d 在 iOS 上推送( push notification ) 编写(unity3d如何发布ios教程)

下一篇:unity学习之NGUI做NPC对话(unity做app)

  • 税收分类编码表下载2023
  • 公司税后利润怎么算
  • 培训机构财务怎么做账
  • 对公账户给私人转账手续费
  • 合并财务报表内部交易对净利润影响
  • 办公室租赁费计入什么科目
  • 订金退回怎么做账
  • 废气处理工程会计分录
  • 工伤费用的伤残补助金属于福利费吗
  • 耕地占用税如何申报缴纳
  • 利润表要怎么看
  • 培训费可以抵扣企业所得税
  • 金税盘可以异地使用吗
  • 地下商铺出售产权归属
  • 纳税信用b
  • 注册资本变更需要去税务局吗
  • 公司注销固定资产如何处理
  • 成本计算错误如何应对
  • windows11进入桌面黑屏
  • 快速迅雷
  • macos 关闭屏幕
  • 贷款利率计算公式表格
  • 销售合作分成会计分录
  • 电脑重装win7系统后无法连接网络
  • 厂区道路施工步骤
  • php接收数据的方法
  • php require的用法
  • 银行本票存款和银行存款的区别
  • 编制记账凭证出现错误
  • 用smart原则改写年底前完善客户资料
  • 买花木计入什么科目
  • 会计申报需要哪些数据
  • php支付宝支付流程原理
  • 【AIGC】6、Segment Anything | Meta 推出超强悍可分割一切的模型 SAM
  • 销售商品怎么做好宣传
  • 二手车折旧法
  • 销售费用的主要类型有哪些
  • 为etc充值
  • 资金结存属于什么会计科目
  • idea快速生成lambda
  • java中file的用法
  • 取得发票没有加税怎么办
  • 企业注销需要清产核资吗
  • 固定资产折旧计算方法公式大全
  • 啤酒的消费税
  • 开发无形资产过程中发生的支出计入什么科目
  • 开发票该怎么操作?
  • 通信服务费账务处理流程
  • 城建税50%减免条件
  • 计提本月工资的账务处理
  • 微信扣除手续费超过多少免费
  • 公司拿房子抵债需要缴纳什么税费
  • 增值税纳税申报表在哪里打印
  • 企业如何科学设计产品
  • mysql5.7.29安装
  • win2003 安装iis
  • rsyslog 8.11.0 (v8-stable) 发布
  • cmd命令 cd
  • win8创建系统映像
  • win8系统如何查看电脑mac物理地址
  • Win8系统网络SSID怎么设置?Win8设置网络SSID的方法
  • win8系统如何升级到win10
  • frameworkservic.exe是什么进程 有什么作用 frameworkservic进程查询
  • linux错误提示
  • eclipse win7
  • python爬取教程
  • Cocos2dx------从json文件读取数据
  • Metaio SDK 6.0.2自带Example自己实现(三)
  • perl and
  • 从头开始绘制一幅画图片
  • mysql如何将查询结果输出到文件
  • jquery可以实现哪些效果
  • node.js详解
  • 详解linux设备
  • jQuery+formdata实现上传进度特效遇到的问题
  • 全年上缴个人所得税多少
  • 重庆车辆检测费多少钱
  • 地税网上预约
  • 江苏省国税务局
  • 中介服务企业怎么注册
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设