Unity 基础 之 一个点(物体)绕另一个点(物体)旋转的简单封装
目录
Unity 基础 之 一个点(物体)绕另一个点(物体)旋转的简单封装
一、简单介绍
Unity中的一些知识点整理。
本节简单介绍在Unity开发中的,因为项目的开发需要,需要把手机屏幕当做触控板,模拟鼠标移动和点击交互等,竖拿手机,横屏画面显示在眼镜上,所以这里简单需要把手机的操作旋转90度转化一下,这里简单记录一下,场景不一样,可能转换不一样,这里仅供参考学习使用,如果你有新的方式也可以留言,多谢。
二、实现原理
1、这里使用 Input.GetMouseButtonDown(0) 、Input.GetMouseButton(0) 、Input.GetMouseButtonUp(0) 相关事件来获取位置相关信息进行处理
2、通过 Quaternion.AngleAxis() 接口进行对应位置旋转转化,得到新的位置
三、注意事项
1、使用场景不同,可能需要做适当的屏幕适配
2、Demo 中 的 UI 元素的 Anchors 在左下角
四、效果预览
五、实现步骤
1、打开 Unity ,新建空工程
2、布置场景,添加两个 Image ,作为 Demo 演示旋转中心,和旋转后的位置显示使用
3、注意两个 Image 的 Anchors 设置为 左下角
4、在工程中添加两个脚本,实现对应逻辑
5、把 TestRotateAround 挂载到场景中,并对应赋值
6、运行场景,效果如上
六、关键代码
1、TestRotateAround
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XANUtil;
namespace XANUtilDemo {
public class TestRotateAround : MonoBehaviour
{
public RectTransform CenterImageRectTrans;
public RectTransform RotatedPosImageTrans;
// Start is called before the first frame update
void Start()
{
RotatedPosImageTrans.gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
Test();
}
void Test() {
if (Input.GetMouseButtonDown(0))
{
RotatedPosImageTrans.gameObject.SetActive(true);
}
else if (Input.GetMouseButton(0))
{
RotatedPosImageTrans.anchoredPosition = ModifyPosValueForGlass(Input.mousePosition);
}
else if (Input.GetMouseButtonUp(0))
{
RotatedPosImageTrans.gameObject.SetActive(false);
}
}
Vector2 ModifyPosValueForGlass(Vector2 pos)
{
Vector3 p = new Vector3(pos.x, 0, pos.y);
Vector3 c = new Vector3(CenterImageRectTrans.anchoredPosition.x, 0, CenterImageRectTrans.anchoredPosition.y);
Vector3 tmp = RotateAroundUtil.RotateRound(p, c, Vector3.up, 90);
Vector2 v = new Vector2(tmp.x, tmp.z);
return v;
}
}
}
2、RotateAroundUtil
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace XANUtil {
public class RotateAroundUtil
{
/// <summary>
/// 一个点绕着另一个点旋转指定度数,并且返回旋转后的点
/// </summary>
/// <param name="position">要旋转的点</param>
/// <param name="center">围绕旋转的点</param>
/// <param name="axis">旋转的轴方向</param>
/// <param name="angle">旋转角度</param>
/// <returns>旋转后得到的点</returns>
public static Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle)
{
Vector3 point = Quaternion.AngleAxis(angle, axis) * (position - center);
Vector3 resultVec3 = center + point;
return resultVec3;
}
}
}
七、附加:让一个物体围绕某一点旋转
1、方法1:B不动,A挂脚本实现transform的RotateAround(vector3 point, vector3 axis, float angle)函数
例如 A.RotateAround(B.position, Vector3.up, 30*Time.deltaTime); //A绕着B的y轴进行旋转。
2)方法2:A不动,A作为B的子物体,B挂脚本实现自转,然后带着A转,类似于模拟围绕中心天体旋转。
例如:B.Rotate (Vector3.up, 30*Time.deltaTime, Space.Self); //B绕着自身的up方向自转,从而带着A一起转。
旋转的常用方法:基于 Transform 组件下
(1)绕坐标轴旋转
public void Rotate (Vector3 eulerAngles, Space relativeTo = Space.Self);
或者
public void Rotate (float xAngle, float yAngle, float zAngleSpace, relativeTo = Space.Self);
其中relativeTo = Space.Self表示绕自身坐标系旋转,ralativeTo = Space.World表示绕世界坐标系旋转。
(2) 饶某个向量旋转
public void Rotate(Vector3 axis, float angle, Space relativeTo);
其中axis为旋转轴方向,angle为旋转角度,relativeTo为参考坐标系,默认为Space.self。此方法的功能是使得GameObject对象在relativeTo坐标系中绕轴向量axis旋转angle度。
(3)绕轴点旋转
public void RotateAround(Vector3 point, Vector3 axis, float angel);
功能是使得GameObject对象绕着point点的axis方向旋转angle度。
参考博客:https://blog.csdn.net/qiaoquan3/article/details/51306514