以下是本篇文章正文内容,下面案例可供参考
一个数字列表,表示各个维度上有位移
//求向量模长
private void Demo01()
{
Vector3 pos = this.transform.position;
//数学公式
float m01 = Mathf.Sqrt(Mathf.Pow(pos.x, 2) + Mathf.Pow(pos.y, 2) + Mathf.Pow(pos.z, 2));
//API:获取向量模长
float m02 = pos.magnitude;
//两点间距离
float m03 = Vector3.Distance(Vector3.zero, pos);
Debug.LogFormat("{0}--{1}--{2}", m01, m02, m03);
}
//获取向量方向也称“标准化向量”,“归一化向量”,即获取该向量的单位向量
private void Demo02()
{
Vector3 pos = this.transform.position;
Vector3 n01 = pos / pos.magnitude;
//API:获取向量的方向 标准化 归一化
Vector3 n02 = pos.normalized;
}
private void Demo03()
{
//向量计算
Vector3 relativeDirction = t1.position - t2.position;
//方向:指向被减向量
//大小:两点间距
//t3沿着relativeDirction移动
//如果relativeDirction不标准化,t3的移动的距离会根据t1与t2之间的距离变化而改变
t3.Translate(relativeDirction.normalized);
t3.position = t3.position + relativeDirction.normalized;
}
代码如下(示例):
private void Demo04()
{
float d1=60;
//角度->弧度
float r1 = d1 * Mathf.PI / 180;
float r2 = d1 * Mathf.Deg2Rad;
//Mathf.Rad2Deg
}
知道一个边和一个角用三角函数;知道两条边用反三角函数
API
注:radian为弧度(Mathf.Deg2Rad)
又称“点积”或“内积”
private void Demo05()
{
//dot两个向量夹角cos值
float dot=Vector3.Dot(t1.position.normalized,t2.position.normalized);
//计算夹角
angle=Mathf.Acos(dot)*Mathf.Red2Deg;
//如果两个向量夹角大于60度的限定条件
//if(angle>60)
//直接判断cos的值
if(dot<0.5)
}
又称“叉积”或“外积”、
private void Demo06()
{
//计算叉乘
Vector3 cross=Vector3.Cross(t1.position,t2.position);
//计算一圈夹角
if(cross.y<0)
{
angle=360-angle;
}
//叉乘所得向量的模长与角度关系:0~90度角
Vector3 cross=Vector3.Cross(a.normalized,b.normalized);
float angle=Mathf.Asin(cross.magnitue)* Mathf.Rad2Deg;
}
练习:计算物体右前方30度,10m远
float x=Mathf.Sin(30*Mathf.Deg2Rad)*10;
float y=Mathf.Cos(30*Mathf.Deg2Rad)*10;
Vector3 worldPoint=transform.TransformPoint(x,0,z);
private void Demo07()
{
//沿x轴旋转
this.transform.eulerAngles+=new Vector3(1,0,0);
this.transform.eulerAngles+=Vector3.up;
}
四元数用法
private void Demo08()
{
//旋转轴
Vector3 axis=Vector3.up;
//旋转弧度
float rad=60*Mathf.Deg2Rad;
Qutaternion qt=new Quaternion();
qt.x=Mathf.Sin(rad/2)*axis.x;
qt.y=Mathf.Sin(rad/2)*axis.y;
qt.z=Mathf.Sin(rad/2)*axis.z;
qt.w=Mathf.Cos(rad/2);
this.transform.rotation=qt;
//欧拉角转换为四元数
this.transform.rotation=Quaternion.Euler(0,60,0);
}
四元数左乘向量,表示将该向量按照四元数表示的角度旋转。
例如:
Vector3 point=new Vector3(0,0,10);
Vector3 newPoint=Quaternion.Euler(0,30,0)*point;
两个四元数相乘可以组合旋转效果
例如:
Quaternion rotation01=Quaternion.Euler(0,30,0)*Quaternion.Euler(0,20,0);
Quaternion rotation02=Quaternion.Euler(0,50,0);
rotation01与rotation02相同
练习:右前方10m远
//(0,0,10)向量根据当前物体的旋转而旋转
Vector3 vect=this.transform.rotation*new Vector3(0,0,10);
//vect 向量沿y轴旋转30度
vect=Quaternion.Euler(0,30,0)*vect;
//vect向量移动到当前物体的位置
vect=this.transform.position+vect;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoomDemo : MonoBehaviour {
public string playerTag = "Player";
private Transform playerTF;
private float radius;
private Vector3 leftTangent, rightTangent;
void Start()
{
GameObject playerGo = GameObject.FindGameObjectWithTag(playerTag);
playerTF = playerGo.transform;
radius = playerGo.GetComponent<CapsuleCollider>().radius;
}
//计算切点
private void CalculateTangent()
{
//爆炸点与玩家之间的距离
Vector3 PlayerToExplosion = this.transform.position - playerTF.position;
//爆炸点与玩家之间的半径长向量
Vector3 PlayerToExplosionDirection = PlayerToExplosion.normalized * radius;
//用反三角函数计算需要旋转的角度
float angle = Mathf.Acos(radius / PlayerToExplosion.magnitude) * Mathf.Rad2Deg;
//半径旋转至切点
//加上玩家的坐标,可以使切点跟随玩家移动
leftTangent = playerTF.position + Quaternion.Euler(0, -angle, 0) * PlayerToExplosionDirection;
rightTangent = playerTF.position + Quaternion.Euler(0, angle, 0) * PlayerToExplosionDirection;
}
void Update()
{
CalculateTangent();
Debug.DrawLine(this.transform.position, leftTangent);
Debug.DrawLine(this.transform.position, rightTangent);
}
}
静态变量
方法
根据用户输入的方向旋转角色,并向前移动
private void Update()
{
float hor=Input.GetAxis("Horizontal");
float ver=Input.GetAxis("Vertical");
if(hor!=0||ver!=0)
{
move(hor,ver);
}
}
private void move(float hor,float ver)
{
Quaternion dir=Quaternion.LookRotation(new Vector3(hor,0,ver));
this.transform.rotation=Quaternion.Lerp(this.transform.rotation,dir,Time.deltaTime*RotateSpeed);
this.transform.Translate(0,0,Time.deltaTime*MoveSpeed);
}
1、World Space
2、 Local Space
3、 Screen Space
4、 Viewport Space
1、Local Space–>World Space
2、World Space–>Local Space
3、World Space<–>Screen Space
4、World Space<–>Viewport Space