Unity绕物体旋转两种方式

更新时间:2023-06-17 13:18:59 阅读: 评论:0

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;
}

本文发布于:2023-06-17 13:18:59,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/148251.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:旋转   物体   跟随   英国   留学
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图