Unity绕物体旋转两种⽅式Unity 绕物体旋转两种⽅式
前⾔
项⽬上遇到⼀个要绕物体旋转的问题,还要可以动态修改旋转中⼼和旋转半径。
第⼀种⽅式
使⽤Unity⾃带的API RotateAround
有两个问题
1. 物体移动的时候⽆法准确跟随
2. ⽆法修改圆的半径
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemScript : MonoBehaviour
{
//中⼼点
public Transform center;
//转动速度
public float rotationSpeed=10;
void Update(){
//跟随center转圈圈
}
}
第⼆种⽅式
人道毁灭
可以动态修改中⼼点的位置和旋转半径
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemScript : MonoBehaviour
{
proceed是什么意思//转动速度
public float speed =1;
//转动的位置
public float progress =0;
benetton//中⼼点
last christmas歌词public Transform center;
//半径
参加音乐会public float radius =3;
void Update(){
progress += Time.deltaTime * speed;
if(progress>=360)
grasp
{
progress -=360;
}
float x1 = center.position.x + radius * Mathf.Cos(progress);
float y1 = center.position.y + radius * Mathf.Sin(progress);
}
}
随机环绕
频道英文
//中⼼点
public Transform center;
//旋转速度
public float rotationSpeed=5;
//半径
public float rotationRadius =2;
//转动的位置
public float progress =0;
public float randAng =0;
public Vector3 randZhou;
private void Start()
antonia{
randAng= Random.Range(0,181);
randZhou =new Vector3(Random.Range(-1f,1f), Random.Range(-1f,1f), Random.Range(-1f,1f)); }
void Update()
{
mobile是什么意思
//跟随center转圈圈
progress += Time.deltaTime * rotationSpeed;
if(progress >=360)
{
progress -=360;
}
float x1 = center.position.x + rotationRadius * Mathf.Cos(progress);
float y1 = center.position.y + rotationRadius * Mathf.Sin(progress);
Vector3 tmp =RotateRound(new Vector3 (x1,y1), center.position, randZhou, randAng);
英国留学 机构ansform.position = tmp;
}
/// <summary>
/// 围绕某点旋转指定⾓度
/// </summary>
/// <param name="position">⾃⾝坐标</param>
/// <param name="center">旋转中⼼</param>
/// <param name="axis">围绕旋转轴</param>
/// <param name="angle">旋转⾓度</param>
/
// <returns></returns>
public Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis,float angle)
{
return Quaternion.AngleAxis(angle, axis)*(position - center)+ center;
}