坐标的表达
//得到点a前方10米处点坐标
Vector3 a= new Vector3(9f,9f,81f);
Vector3 b = a + Vector3.forward * 10f; //点b =点a + 方向 * 距离;
transform.forward 表示角色前方方向(是一个向量而不能表示前方3D坐标点。
向量为什么不能表示明确的3D坐标点呢,一般向量值在正负(0.001-1)之间,如果值表示3D坐标点的话 也就是在0,0,0零起点附近,也就是向量几乎等于零起点了。
使A物体的前方对向 B物体的上方
2D的方向
各方向的表达
3D的方向
还要加上方向和下方向
====1===
物体A前后左右上下7米处坐标表达
using UnityEngine;
public class Test_02 : MonoBehaviour
{
bool runonce = true;
public void F0_1()
{
var r = transform.rotation;
var forward = transform.position + transform.forward * 7f;//物体A前方7米的坐标.
var back = transform.position + (r * Vector3.back) * 7f;
var left = transform.position + (r * Vector3.left) * 7f;
var right = transform.position + transform.right * 7f;
var up = transform.position + transform.up * 7f;
var down = transform.position + (r * Vector3.down) * 7f;
Quaternion r0 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y + 45f, r.eulerAngles.z);
var backLeft = (transform.position + (r0 * Vector3.back) * 7f); ;
var r1 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y - 45f, r.eulerAngles.z);
var backRight = (transform.position + (r1 * Vector3.back) * 7f); ;
var r2 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y - 45f, r.eulerAngles.z);
var forwardLeft = (transform.position + (r2 * Vector3.forward) * 7f); ;
var r3 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y + 45f, r.eulerAngles.z);
var forwardRight = (transform.position + (r3 * Vector3.forward) * 7f); ;
Debug.DrawLine(transform.position, forward, Color.blue);
Debug.DrawLine(transform.position, back, Color.black);
Debug.DrawLine(transform.position, left, Color.gray);
Debug.DrawLine(transform.position, right, Color.red);
Debug.DrawLine(transform.position, up, Color.green);
Debug.DrawLine(transform.position, down, Color.white);
Debug.DrawLine(transform.position, forwardLeft, Color.cyan);
Debug.DrawLine(transform.position, forwardRight, Color.magenta);
Debug.DrawLine(transform.position, backLeft, Color.green);
Debug.DrawLine(transform.position, backRight, Color.yellow);
if(runonce)
{
runonce = false;
DrawPoint(forwardLeft, "forwardLeft", Color.cyan);
DrawPoint(forwardRight, "forwardRight", Color.magenta);
DrawPoint(backLeft, "backLeft", Color.green);
DrawPoint(backRight, "backRight", Color.yellow);
Debug.Log(Vector3.Distance(forwardLeft, transform.position));
}
}
// Update is called once per frame
void Update()
{
F0_1();
}
public static Transform DrawPoint(Vector3 pos, string namee = "1" ,Color color = default(Color), float duration = float.MaxValue)
{
var scale = 0.08f;
var temp = GameObject.CreatePrimitive(PrimitiveType.Sphere);
temp.transform.position = pos;
temp.transform.localScale = new Vector3(scale, scale, scale);
temp.GetComponent<Renderer>().material.color = color;
temp.gameObject.name = namee;
var light = temp.AddComponent<Light>();
light.type = LightType.Point;
light.color = color;
Destroy(temp.GetComponent<Collider>());
Destroy(temp, duration);
return temp.transform;
}
}
=====2===
角色绕Y轴80度旋转,再向前方移动5米后的坐标
float angle = 80f;
float length = 5f;
Quaternion q = Quaternion.Euler(transform.rotation.eulerAngles.x,angle, transform.rotation.eulerAngles.z); ///求出第i个点的旋转角度
Vector3 Point = transform.position +(q*transform.forward) * length;
更多注释
//360代表(360度,把360分成多少分,第几份).如果a = 4前方a=2后方a= 3右方a=1左方
//Quaternion q = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y - (360/4 * a), transform.rotation.eulerAngles.z); ///求出第i个点的旋转角度
void Update()
{
float angle = 80f;
float length = 5f;
Quaternion q = Quaternion.Euler(transform.rotation.eulerAngles.x, angle, transform.rotation.eulerAngles.z); ///求出第i个点的旋转角度
Vector3 Point = transform.position + (q * transform.forward) * length;
Debug.DrawLine(transform.position, Point, Color.yellow);
}
进阶改成函数
例子:求出A物体右方10米处的点坐标是多少?
var point = DirC(A.transform, dirE.right, 10f);
/// <summary>
///求出A物体右方10米处的点坐标
/// </summary>
public static class DiritionCaculaterEx
{
public enum DirE
{
forward,
back,
left,
right,
up,
down,
forwardLeft,
forwardRight,
backLeft,
backRight,
}
/*
* 例子:求出A物体前方7米处的点坐标是多少?
* var point = DirC(A.transform, dirE.right, 10f);
*/
/// <summary>
///求出A物体前方7米处的点坐标
/// </summary>
/// <param name="transform">位置</param>
/// <param name="dir">方向</param>
/// <param name="distance">距离</param>
/// <returns></returns>
public static Vector3 DirC(Transform transform, DirE dir = DirE.forward, float distance = 7f)
{
var r = transform.rotation;
var forward = transform.position + transform.forward * distance;
var back = transform.position + (r * Vector3.back) * distance;
var left = transform.position + (r * Vector3.left) * distance;
var right = transform.position + transform.right * distance;
var up = transform.position + transform.up * distance;
var down = transform.position + (r * Vector3.down) * distance;
Quaternion r0 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y + 45f, r.eulerAngles.z);
var backLeft = (transform.position + (r0 * Vector3.back) * distance);
var r1 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y - 45f, r.eulerAngles.z);
var backRight = (transform.position + (r1 * Vector3.back) * distance);
var r2 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y - 45f, r.eulerAngles.z);
var forwardLeft = (transform.position + (r2 * Vector3.forward) * distance);
var r3 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y + 45f, r.eulerAngles.z);
var forwardRight = (transform.position + (r3 * Vector3.forward) * distance);
switch (dir)
{
case DirE.forward:
return forward;
break;
case DirE.back:
return back;
break;
case DirE.left:
return left;
break;
case DirE.right:
return right;
break;
case DirE.up:
return up;
break;
case DirE.down:
return down;
break;
case DirE.forwardLeft:
return forwardLeft;
break;
case DirE.forwardRight:
return forwardRight;
break;
case DirE.backLeft:
return backLeft;
break;
case DirE.backRight:
return backRight;
break;
}
return transform.position;
}
/// <summary>
/// 求出A物体前方10米处的点坐标
/// </summary>
/// <param name="A"></param>
/// <param name="dir"></param>
/// <param name="distance"></param>
/// <returns></returns>
public static Vector3 DirC(GameObject A, DirE dir = DirE.forward, float distance = 10f)
{
var r = A.transform.rotation;
var forward = A.transform.position + A.transform.forward * distance;
var back = A.transform.position + (r * Vector3.back) * distance;
var left = A.transform.position + (r * Vector3.left) * distance;
var right = A.transform.position + A.transform.right * distance;
var up = A.transform.position + A.transform.up * distance;
var down = A.transform.position + (r * Vector3.down) * distance;
Quaternion r0 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y + 45f, r.eulerAngles.z);
var backLeft = (A.transform.position + (r0 * Vector3.back) * distance);
var r1 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y - 45f, r.eulerAngles.z);
var backRight = (A.transform.position + (r1 * Vector3.back) * distance);
var r2 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y - 45f, r.eulerAngles.z);
var forwardLeft = (A.transform.position + (r2 * Vector3.forward) * distance);
var r3 = Quaternion.Euler(r.eulerAngles.x, r.eulerAngles.y + 45f, r.eulerAngles.z);
var forwardRight = (A.transform.position + (r3 * Vector3.forward) * distance);
switch (dir)
{
case DirE.forward:
return forward;
break;
case DirE.back:
return back;
break;
case DirE.left:
return left;
break;
case DirE.right:
return right;
break;
case DirE.up:
return up;
break;
case DirE.down:
return down;
break;
case DirE.forwardLeft:
return forwardLeft;
break;
case DirE.forwardRight:
return forwardRight;
break;
case DirE.backLeft:
return backLeft;
break;
case DirE.backRight:
return backRight;
break;
}
return A.transform.position;
}
/// <summary>
/// 求出A物体Y轴80度7米远的点坐标
/// </summary>
/// <param name="go">GameObject</param>
/// <param name="angle">角度</param>
/// <param name="distance">距离</param>
/// <returns></returns>
public static Vector3 GetAnglePoint(GameObject A, float angle = 80f, float distance = 7f)
{
var r = A.transform.rotation;
var a_point = A.transform.position;
Quaternion q = Quaternion.Euler(r.eulerAngles.x, angle, r.eulerAngles.z); ///求出第i个点的旋转角度
Vector3 point = a_point + (q * A.transform.forward) * distance;
//Debug.DrawLine(a_point, point, Color.yellow);
return point;
}
}
求出A物体Y轴80度7米远的点坐标?
全圆仪_逆时针.bmp
全圆仪_逆时针.PNG
全圆仪_顺时针.bmp
全圆仪_顺时针.PNG
我们以角色的Z轴方向为0度,通过全圆仪_顺时针量出轴到想要的点的度数
using UnityEngine;
public class Test_03 : MonoBehaviour
{
/// <summary>
/// 角度
/// <summary>
[Header("角度")]
[Range(0.0f, 360.0f)]
public float angle = 80f;
/// <summary>
/// 距离
/// <summary>
[Header("距离")]
[Range(1f, 15)]
public float distance = 7f;
void Update()
{
var r = transform.rotation;
Quaternion q = Quaternion.Euler(r.eulerAngles.x, angle, r.eulerAngles.z); ///求出第i个点的旋转角度
Vector3 Point = transform.position + (q * transform.forward) * distance;
Debug.DrawLine(transform.position, Point, Color.yellow);
}
}