- Home /
How can i make an object to rotate in a circle smooth around another object nonstop ?
What i want is a script to move the Cube(2) around the Turret and to be close to the Turret in circles nonstop automatic. The Cube(2) is the object to move in circles. The turret is the Cube. Just moving the Cube(2) is not a problem but how to make it move in a smooth circles around the turret(Cube) ?
And the height of Cube(2) not should change i mean close to the turret(Cube) can be also up in the air so it will move in circles in this height. The Cube(2) Y position not should be change. Just the circles he move in to be small and too big radius.
Answer by RisingDead_xTR · Aug 07, 2017 at 08:04 AM
Here is the code you need. It is well defined. Tell me if something goes wrong :)
public class Answer : 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.
// 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);
}
}
Hope this helps :)
Sorry.
This is working perfect. I just have another sub question:
If i want to make the Cube(2) to move around in circle like it is now but also to make that it will change it's height in random range while moving in circle ?
I tried to add this line in the Update
transform.position = new Vector3(0, Random.Range(0,5), 0);
void Update()
{
transform.position = new Vector3(0, Random.Range(0,5), 0);
//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);
}
But it's not working the Cube(2) is just vanished/deleted.
I want the cube move around in circle and also like floating up down random while moving in circle.
Sorry for the late reply. Here is the script to float your object up and down randomly while moving in circles.
using UnityEngine;
public class Answer : $$anonymous$$onoBehaviour
{
// 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;
}
//$$anonymous$$athf.Lerp changes height from 'prevHeight' to 'height' gradually (smooth transition)
transform.position=new Vector3(transform.position.x, $$anonymous$$athf.Lerp(prevHeight,height,time), transform.position.z);
}
}
Notify me if something goes wrong .
Answer by tesan · Aug 09, 2017 at 07:38 PM
you can watch this plugin: http://u3d.as/Un7, I think it's what you want