[英]How can I move some object in circle around another object?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetBehaviour : MonoBehaviour
{
// Add this script to Cube(2)
[Header("Add your turret")]
public GameObject Turret;//to get the position in worldspace to which this gameObject will rotate around.
[Header("The axis by which it will rotate around")]
public Vector3 axis;//by which axis it will rotate. x,y or z.
[Header("Angle covered per update")]
public float angle; //or the speed of rotation.
public float upperLimit, lowerLimit, delay;// upperLimit & lowerLimit: heighest & lowest height;
private float height, prevHeight, time;//height:height it is trying to reach(randomly generated); prevHeight:stores last value of height;delay in radomness;
// Update is called once per frame
void Update()
{
//Gets the position of your 'Turret' and rotates this gameObject around it by the 'axis' provided at speed 'angle' in degrees per update
transform.RotateAround(Turret.transform.position, axis, angle);
time += Time.deltaTime;
//Sets value of 'height' randomly within 'upperLimit' & 'lowerLimit' after delay
if (time > delay)
{
prevHeight = height;
height = Random.Range(lowerLimit, upperLimit);
time = 0;
}
//Mathf.Lerp changes height from 'prevHeight' to 'height' gradually (smooth transition)
transform.position = new Vector3(transform.position.x, Mathf.Lerp(prevHeight, height, time), transform.position.z);
}
}
In general it's working the problem is for example if I set the axis x,y,z to 1,1,1 on the variable: axis 总的来说,它起作用了,例如,如果我在变量:axis上将x,y,z轴设置为1,1,1
And Angle set to 1 Upper limit to 50 Lower limit to 2 and delay to 2. 角度设置为1上限设置为50下限设置为2,延迟设置为2。
Then the object is making a circle around the other object but sometimes when the object is getting higher the most he get higher the object making a bigger circle and then when the object is getting lower the circle is smaller. 然后,该对象在另一个对象上绕一个圆,但是有时,当对象变得越来越高时,他越靠近对象,则形成一个更大的圆;然后,当对象变得越低时,该圆圈就会变小。
How can I make it to keep the circle radius static ? 如何使其保持圆半径不变?
The main goal is to move the object in circles around another object in random highs limits for example 2 and 50 but I want to keep the same radius all the time. 主要目标是以随机上限(例如2和50)在另一个对象周围绕圈移动对象,但我想一直保持相同的半径。 Now the radius is changing by depending on the height. 现在,半径根据高度而变化。
当您不断向上移动对象时,如果希望旋转半径保持恒定,则旋转轴必须垂直,即Vector3.up
或new Vector3(0, 1, 0)
Vector3.up
new Vector3(0, 1, 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.