已解决
Untiy 使用RotateAround()方法实现物体围绕某个点或者某个物体旋转
来自网友在路上 11008100提问 提问时间:2023-11-19 22:40:10阅读次数: 100
最佳答案 问答题库1008位专家为你答疑解惑
Untiy 实现物体围绕指定点或者某个物体旋转,可使用RotateAround()方法。
语法:
public void RotateAround(Vector3 point, Vector3 axis, float angle);
其中,point:旋转中心点位置;
axis:要围绕的轴,如x,y,z
angel:旋转的角度
该方法可以实现物体围绕指定的中心点和轴旋转。
下面举例实现围绕某个点旋转和围绕某个物体旋转。
1、围绕某个点旋转。
在场景中创建一个球体。然后编写以下脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RotateTest : MonoBehaviour
{public float rotationSpeed = 0.01f;public Transform tran;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){tran.RotateAround(new Vector3(10f,0f,0f), Vector3.up* rotationSpeed, 1f);}
}
把脚本放到场景中,并把球体拉到tran参数中,运行场景,球体就会围绕着坐标为(10f,0f,0f),Y轴方向旋转,每帧旋转角度为1。
这里围绕的轴常用的是围绕X轴、Y轴、Z轴,表示法可参考以下:
//X轴表示法
Vector3.right
new Vector3(1,0,0)//Y轴表示法
Vector3.up
new Vector3(0,1,0)//Z轴表示法
Vector3.forward
new Vector3(0,0,1)
2、围绕某个物体旋转,方法大抵相同,主要是要把第一个参数设置为物体的坐标,第二个参数为沿着物体的轴。
同样创建一个球体和一个物体:
创建以下代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RotateTest : MonoBehaviour
{public float rotationSpeed = 0.01f;、、public Transform tran;public Transform tran1;public Transform Obj;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){//tran.RotateAround(new Vector3(10f,0f,0f), Vector3.up* rotationSpeed, 1f); //围绕点旋转tran1.RotateAround(Obj.position, Obj.up * rotationSpeed, 1f); //围绕物体旋转}
}
把脚本拉到场景中,把球体赋予tran1,物体赋予Obj,那么就可以实现球体围绕着物体旋转。
最终效果:
Untiy 实现物体围绕指定点或者某个物体旋转
查看全文
99%的人还看了
相似问题
- Untiy 使用RotateAround()方法实现物体围绕某个点或者某个物体旋转
- 【OpenCV】计算视频的光流并跟踪物体calcOpticalFlowPyrLK
- 双十一“静悄悄”?VR购物拉满沉浸式购物体验
- 【Unity ShaderGraph】| 物体靠近时局部溶解,根据坐标控制溶解的位置【文末送书】
- 最新Cocos Creator 3.x 如何动态修改3D物体的透明度
- unity 从UI上拖出3D物体,(2D转3D)
- DirectX3D 虚拟现实项目 三维物体的光照及着色(五个不同着色效果的旋转茶壶)
- Azure 机器学习 - 使用 AutoML 和 Python 训练物体检测模型
- 【UE】从UI中拖拽生成物体
- RK3588平台开发系列讲解(项目篇)基于yolov5的物体识别
猜你感兴趣
版权申明
本文"Untiy 使用RotateAround()方法实现物体围绕某个点或者某个物体旋转":http://eshow365.cn/6-39715-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: 闭眼检测实现
- 下一篇: 美国服务器:全面剖析其主要优点与潜在缺点