我们在项目中经常会遇到是选择用transform.foward和vector3.foward以及其他方向的坐标
- transform.(方向),这个是自身坐标系,后边只能跟foward,right,up,而其他的三个方向我们可以用负号去表示
vector3.(方向),这个是世界坐标系,后边可以跟六个方向这里我就不多说了。
他们两个都是一个单位向量 - 对于自身坐标和世界坐标我一直不甚理解,感觉他们就是一个单位向量,甚至有的时候觉得他们就是一样的,等到做了一个小项目以后才发现他们的区别下边我们就以foward为例来说明
- 物体自身坐标系和世界坐标系重合

transform.foward就是(0,0,1),而vector3.foward也是(0,0,1),他们两个是完全一样的。
- 物体自身坐标系和世界坐标系不一样

上图是以Y轴为基础旋转90,此时世界坐标系和自身坐标系就不一样了,此时的vector3.foward是(0,0,1),而此时的transform.foward就是(1,0,0),而这个transform.foward的值是怎么来的呢?每个物体都有自己的朝向,我们按照他的前方方向可以找到一个点就箭头1指向的那个点,而这个点在世界中的坐标系就是(1,0,0),这个只是跟物体自身的旋转角度有关系,与他的position是没有关系的。

这次我旋转了45度,transform.foward我们自己心算下就是(1/√2,0,1√2),我们用代码输出一下transform.foward

因为这个四舍五入,所以输出的是(0.7,0,0.7),所以我们以后自己注意下就好。
相对坐标问题


这个相对坐标是世界坐标,它的运动方向是按照物体自身的前方移动的,也就是世界坐标的右方
但是加入我们把相对坐标换下

这次我们的相对坐标是自身坐标系,而此时物体的运动方向

运动方向的参数都是transform.forward,但是运动方向却不一样,相对于世界坐标系的大家都好理解,但是相对自身坐标系的运动方向去不是那么好理解了。相对于世界坐标系的时候,我们相当于把transform.forward转化了一次,相对于自身坐标系的时候,我们就需要把那个参数转化两次,说起来有点拗口,先把自身的坐标系当做是世界坐标系,那transform.forward就是(1,0,0),而我们的相对坐标系就自身坐标系,我们再转化一次,上图就是再顺时钟旋转90度,运动方向相对于真正的世界坐标系就是(0,0,-1)。
箭头1就是转换的第一次的transform.forward,箭头2就是转换的第二次的transform.forward

网友评论