unity3D脚本学习(一)
-
Accelerationevent(物速度事件描述设备加速度状态。
-
Vector3(三维向量)
表示三D的向量和点该结构在 unity中传递3D的位置和方向。也包含做些向量运算的函数。
(1)Vector3. x Vector3.y Vector3.z表示向量的x,y,z组件
(2)Vector. this int[ index]使用[0][1][2]分别访问 Vector3的x,y,z组件.
简单来说就是用索引号代替x,y,z组件。
示例`:using UnityEngine; using System.Collections; public class example : MonoBehaviour { public Vector3 p; public void Awake() { p[1] = 5;//等同于p.y=5; } }
(3)Vector.normalized(规范化)
返回向量的长度为1(只读)。
当规格化后,向量保持同样的方向,但是长度变为1.0。
注意,当前向量是不改变的并且返回一个新的规范化的向量。
如果你想规范化 当前向量,使用Normalize函数。
如果这个向量太小而不能被规范化,一个零向量将会被返回。
(4)Vector.magnitude (长度)
返回向量的长度,及(xx+yy+z*z)的平方根。
(5) Vector.sqrMagnitude(长度的平方)
返回向量的长度的平方。
(6) Vector.MoveToward(移向)
当前的地点移向目标。
(7)Vector3.RotateTowards(转向)
当前向量转向目标。
(8)Vector3.SmoothDamp 平滑阻尼
随着时间的推移,逐渐改变一个向量朝向预期的目标。
最常见的用途是平滑跟随相机
current当前的位置
targetThe我们试图接近的位置
currentVelocity当前速度,这个值由你每次调用这个函数时被修改
smoothTime到达目标的大约时间,较小的值将快速到达目标
maxSpeedOptionally 选择允许你限制的最大速度
deltaTimeThe
自上次调用这个函数的时间。默认为 Time.deltaTime
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform target;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
void Update() {
Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10));
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, refvelocity, smoothTime);
}
}
(9)Vector.Angle角度
由from和to两者返回一个角度。
形象的说,from和to的连线和它们一个指定轴向的夹角
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform target;
void Update() {
Vector3 targetDir = target.position - transform.position;
Vector3 forward = transform.forward;
float angle = Vector3.Angle(targetDir, forward);
if (angle < 5.0F)
print("close");
}
}
(10Vector.Distance 距离·
返回a,b两点之间的距离。
Vector3.Distance(a,b) 等同于(a-b).magnitude 。
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform other;
public void Awake() {
if (typeof(other)) {
float dist = Vector3.Distance(other.position, transform.position);
print("Distance to other: " + dist);
}
}
}
(11)Vector3.Min 最小 . Max类似
返回一个由两个向量的最小组件组成的向量
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Vector3 a = new Vector3(1, 2, 3);
public Vector3 b = new Vector3(4, 3, 2);
public void Awake() {
print(Vector3.Min(a, b));// prints (1.0,2.0,2.0)
}
}
(12)Vector3.operator + 运算符 加法 -同
两向量相加,对应组件加在一起。
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public void Awake() {
print(new Vector3(1, 2, 3) + new Vector3(4, 5, 6));
// prints (5.0,7.0,9.0)
}
}
(13)Vector3.operator * 运算符 乘法 /同
由一个数乘以一个向量。由数字d乘以a的每一个组件
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public void Awake() {
print(2.0F * new Vector3(1, 2, 3));
//使该向量变长2倍 prints (2.0,4.0,6.0)
}
(14)Vector3.operator == 运算符 等于
如果两个向量相等,返回真。
对于非常接近相等向量,也将返回真
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform other;
public void Awake() {
if (other && transform.position == other.position)
print("I'm at the same place as the other transform!");
}
}