Unity3D学习笔记(七):叉乘和四元素
public static Quaternion Euler(Vector3 euler);
public static Quaternion Euler(float x, float y, float z);
public static Quaternion LookRotation(Vector3 forward);
public static Quaternion LookRotation(Vector3 forward, [DefaultValue("Vector3.up")] Vector3 upwards);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LerpTest : MonoBehaviour
{
public Transform sphere;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//API:Vector3.Lerp,线性插值,起始位置from,目标位置to,每次插剩余距离(from-to)的百分之多少t
transform.position = Vector3.Lerp(transform.position, sphere.position, 0.05f);
//Lerp的匀速移动
transform.position = Vector3.Lerp(transform.position, sphere.position, /Vector3.Distance(transform.position,sphere.position) * 0.05F);
//Lerp的匀速旋转
//API:Mathf.Lerp();
}
}
判断方位
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyLookPlayer : MonoBehaviour {
public Transform player;
public float result1;
public float result2;
// Use this for initialization
void Start () {
Vector3 forwardVector = transform.forward;
Vector3 enemy2Player = player.position - transform.position;
//result1 = enemy2Player.x * forwardVector.x + enemy2Player.y * forwardVector.y + enemy2Player.z + forwardVector.z;
result1 = Vector3.Dot(forwardVector, enemy2Player);
Vector3 result = Vector3.Cross(forwardVector, enemy2Player);
result2 = result.y;
} // Update is called once per frame
void Update () { }
public void OnGUI()
{
if (result1 > )
{
if (result2 > )
{
GUILayout.Label("玩家在我的方位:右前方");
}
else
{
GUILayout.Label("玩家在我的方位:左前方");
}
}
else
{
if (result2 > )
{
GUILayout.Label("玩家在我的方位:右后方");
}
else
{
GUILayout.Label("玩家在我的方位:左后方");
}
} }
}
路点移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FindPath : MonoBehaviour {
public Transform[] paths;
public Vector3 dir;
public float moveSpeed;
public float rotSpeed;
int index = ;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
if (Vector3.Distance(paths[index].position, transform.position) <= 0.5f)
{
index++;
index %= paths.Length;
}
dir = paths[index].position - transform.position;
transform.Translate(dir *Time.deltaTime* moveSpeed,Space.World);
Quaternion targetRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation,/ Quaternion.Angle(transform.rotation, targetRotation)* rotSpeed);
}
}
用单例类来管理路点
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathManager : MonoBehaviour {
//用单例类来管理路点
public static PathManager instanse = null;
public static PathManager Instanse
{
get
{
return instanse;
}
}
int index = ;
void Awake()
{
instanse = this;
}
// Use this for initialization
void Start () {
//transform.GetChild(1);
}
// Update is called once per frame
void Update () { }
public Vector3 GetCurrentPos()
{
return transform.GetChild(index).position;
}
public bool IsReached(Vector3 targetPos)
{
Vector3 currPos = GetCurrentPos();
//忽略路点的高度
currPos.y = targetPos.y;
//根据步径调整0.5f的值
return Vector3.Distance(currPos, targetPos) < 0.5f;
}
public void MoveNext()
{
index++;
//index = index % transform.childCount;
index %= transform.childCount;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuaternionTest : MonoBehaviour
{
public Transform girl;
// Use this for initialization
void Start()
{
//把旋转量赋值给transform.rotation
transform.rotation = new Quaternion(, Mathf.Sin( * Mathf.Deg2Rad), , Mathf.Cos( * Mathf.Deg2Rad));
//用乘法表示旋转量叠加
transform.rotation *= new Quaternion(, Mathf.Sin( * Mathf.Deg2Rad), , Mathf.Cos( * Mathf.Deg2Rad)); //API:Quaternion.AngleAxis,输入轴角对儿,返回四元数,如绕着y轴旋转90度
transform.rotation = Quaternion.AngleAxis(, Vector3.up);
//用乘法表示旋转量叠加,先绕着y轴旋转45度,再绕着y轴旋转90度,结果是-225(135)
transform.rotation = Quaternion.AngleAxis(, Vector3.up) * Quaternion.AngleAxis(, Vector3.up); //四元数不满足乘法交换律
//先绕着y轴旋转45度,再绕着x轴旋转45度,结果是(45,45,0)
transform.rotation = Quaternion.AngleAxis(, Vector3.up) * Quaternion.AngleAxis(, Vector3.right);
//先绕着x轴旋转45度,再绕着y轴旋转45度
transform.rotation = Quaternion.AngleAxis(, Vector3.right) * Quaternion.AngleAxis(, Vector3.up);
}
// Update is called once per frame
void Update()
{
//API:Quaternion LookRotation(Vector3 forward);
Vector3 dir = girl.position - transform.position;
//transform.rotation = Quaternion.LookRotation(dir);
//目标位置
Quaternion targetRotation = Quaternion.LookRotation(dir);
//Slerp球形插值,每次0.01f慢慢插
//transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 0.01f);
//
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, / Quaternion.Angle(transform.rotation, targetRotation));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public float moveSpeed = ;
public float rotSpeed = ;
public float radius = ;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 targetPos = PathManager.Instanse.GetCurrentPos();
Vector3 moveDir = targetPos - transform.position;
moveDir.y = ;
//目标与主角y值一致
targetPos.y = transform.position.y;
//移动
transform.position = Vector3.Lerp(transform.position, targetPos, / Vector3.Distance(transform.position, targetPos) * moveSpeed);
//旋转
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDir), / Quaternion.Angle(transform.rotation, Quaternion.LookRotation(moveDir)) * rotSpeed);
//画线
Debug.DrawLine(transform.position, targetPos, Color.green);
//判断是否达到
if (PathManager.Instanse.IsReached(transform.position))
{
PathManager.Instanse.MoveNext();
}
}
private void OnDrawGizmos()
{
Gizmos.color = new Color(, , , 0.3f);
Gizmos.DrawSphere(transform.position, radius);
}
}
补充内容
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class w06d3 : MonoBehaviour {
public Transform target;
public Transform cam;
public Vector3 camerPosOffset;
public float hOffset;
public float moveSpeed = ;
// 1、向量加法的几何意义
// 2、向量减法的几何意义
// 3、向量的点乘的几何意义
// a.b = a.x * b.x + a.y * b.y + a.z * b.z
// a.b = |a| * |b| * cos<a, b>
// a.b = (|b| * cos<a, b>) * |a|
// a.b = (|a| * cos<a, b>) * |b| a向量在b向量上的投影长度 * b向量的模长
// b是单位向量的话, a向量在b向量上的投影长度
// a、b 都是单位向量的话,两个向量的夹角的余弦值
void Start () { if( Vector3.Angle(transform.forward, target.position - transform.position) < )
{
}
} void Update () {
cam.position = transform.position + camerPosOffset;
cam.LookAt(transform.position + Vector3.up * hOffset);
transform.Translate((target.position - transform.position).normalized * moveSpeed * Time.deltaTime, Space.World); }
private void OnTriggerEnter(Collider other)
{ }
private void OnCollisionEnter(Collision collision)
{ }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pool<T> where T : UnityEngine.Object
{
public readonly int Capacity = ;
List<T> items = new List<T>();
public Pool(int capacity = )
{
this.Capacity = capacity;
}
public bool Push(T item)
{
if (items.Count >= Capacity - ) return false;
items.Add(item); return true;
}
public T Pop()
{
if (items.Count == ) return default(T);
T item = items[items.Count - ];
items.RemoveAt(items.Count - );
return item;
}
}
Unity3D学习笔记(七):叉乘和四元素的更多相关文章
- Unity3D学习笔记(八):四元素和书籍推荐
书籍推荐: 3D数学基础:图形与游戏开发——游戏软件开发专家系列(美)邓恩 Unity Shader入门精要 冯乐乐(92年) 数据结构(Python语言描述) 数据结构.算法与应用(C++语言描述) ...
- Typescript 学习笔记七:泛型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- iOS 学习笔记七 【博爱手把手教你使用2016年gitHub Mac客户端】
iOS 学习笔记七 [博爱手把手教你使用gitHub客户端] 第一步:首先下载git客户端 链接:https://desktop.github.com 第二步:fork 大神的代码[这里以我的代码为例 ...
- Linux学习笔记(七) 查询系统
1.查看命令 (1)man 可以使用 man 命令名称 命令查看某个命令的详细用法,其显示的内容如下: NAME:命令名称 SYNOPSIS:语法 DESCRIPTION:说明 OPTIONS:选项 ...
- go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer)
目录 go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer) demo demo server demo client 池 dao service p2c ro ...
- Java IO学习笔记七:多路复用从单线程到多线程
作者:Grey 原文地址:Java IO学习笔记七:多路复用从单线程到多线程 在前面提到的多路复用的服务端代码中, 我们在处理读数据的同时,也处理了写事件: public void readHandl ...
- Unity3D学习笔记2——绘制一个带纹理的面
目录 1. 概述 2. 详论 2.1. 网格(Mesh) 2.1.1. 顶点 2.1.2. 顶点索引 2.2. 材质(Material) 2.2.1. 创建材质 2.2.2. 使用材质 2.3. 光照 ...
- Unity3D学习笔记8——GPU实例化(3)
目录 1. 概述 2. 详论 2.1. 自动实例化 2.2. MaterialPropertyBlock 3. 参考 1. 概述 在前两篇文章<Unity3D学习笔记6--GPU实例化(1)&g ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
随机推荐
- CentOS工作内容(五)单一网卡配置多个IP
CentOS工作内容(五)单一网卡配置多个IP 用到的快捷键 tab 自动补齐(有不知道的吗) ctrl+a 移动到当前行的开头(a ahead) ctrl+e 移动到当前行的开头(e end) ct ...
- Jquery WeUI(一)
用于微信端的控件UI , 首先,需要做的是开发一个微信能访问的网页,并和微信关联 A. 创建一个空网站 B. 增加一般处理程序 A. 增加 web 网页 和空文件到项目中 B. 申请和配置测试服务 创 ...
- js-jquery-对象与JSON字符串互相转换
1:jQuery插件支持的转换方式 代码如下: String→Object$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转 ...
- 【剑指offer】 二叉树中和为某一值的路径
一.题目: 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度 ...
- 11.sklearn.preprocessing.LabelEncoder的作用
In [5]: from sklearn import preprocessing ...: le =preprocessing.LabelEncoder() ...: le.fit(["p ...
- There are 2 missing blocks. The following files may be corrupted
There are 2 missing blocks. The following files may be corrupted: 步骤1,检查文件缺失情况 可以看到, blk_1074785806 ...
- python安装HTMLTestRunner
== https://pypi.org/project/html-testRunner/#files 下载 放在这路径下 cmd中进行安装
- python 一个.py文件如何调用另一个.py文件中的类和函数
原文地址https://blog.csdn.net/winycg/article/details/78512300 在同一个文件夹下 调用函数:
- iOS UI基础-2.0按钮操作与形变
按钮状态 1.normal:默认状态 Default 对应的枚举常量:UIControlStateNormal 2.highlighted(高亮状态) 按钮被按下去的时候(未松开) 对应的枚举常量 ...
- ReactNative前端开发者
ReactNative前端开发者 文档版本0.0.2 Author: Necfol 说明: 本文档用于指导前端React Native的开发,如需开发其他其他框架应用,不适用本文档 前期准备 Reac ...