- Home /
How do you orbit an object over a point on the map in a circle, with the object facing in the correct direction?
I have created a helicopter over terrain (say at y=50). I have a terrain that is 500x 500. I would like the helicopter to orbit with a radius of 50 (available for change above the point in a variable). I would like to have a speed variable that I can set set based on how it looks.
I need it to orbit at the same altitude in a circle in the center of the map (250,0,250), with the helicopter rotating to always face forward on the flight path.
I have some orbiting code here that makes the helicopter go in a circle, but I do not know how to face the helicopter forward along the flight path. I am not fully happy with the code, as when I tweak settings it also seems to make the circle elongate and have strange behaviour based on various combinations of speed, angle rotation, and radius. Additionally, this code does not orbit around the point but in a circle to the north of it (along the z axis forward). Please recommend some easier code or tweaks!!!! Thank you!!
#pragma strict
var speed = .5; // speed .5 seems to work, with radius 20, and change angle 10, but it seems eliptical
var radius = 20.0;
var angle = 0.0;
var changeAngle=10; // change the Angle around the cirlce
var StartX = 250; // origin points for circle
var StartY =250;
var x=0.0;
var y=0.0;
function Update () {
// this is the code to move it in a circle
x = (radius * Mathf.Cos(angle));
y= -(radius * Mathf.Sin(angle));
//Latest
transform.Translate(x*speed*Time.deltaTime,y*speed*Time.deltaTime,0); // this seems to work moving in circle of right radius
angle = angle + changeAngle* Mathf.Deg2Rad * speed*Time.deltaTime;
//Need some lookat code here
}
Answer by robertbu · Mar 22, 2013 at 05:53 PM
In Unity there are a number of simpler ways to accomplish the same task. Attach the following script to your helicopter. Set the pivot to the position in the air around which you want your helicopter to rotate. Place your helicopter at one position on the circle out from the pivot.
var speed : float = 30.0;
private var pivot : Vector3 = new Vector3(0,50,0);
function Update () {
transform.RotateAround(pivot, Vector3.up, speed * Time.deltaTime);
}
If you are satisfied, click the checkmark next to the answer to mark it as having been answered.
Thanks robertbu, I also found this helpful. I was overcomplicating it in my thoughts, as I was unaware of RotateAround function
Your answer
Follow this Question
Related Questions
How do I combine Orbiting with mouse drag and with a button click? 0 Answers
Move object from A to B around Zero 1 Answer
Rotate camera to object on sphere 1 Answer
Touch mouseorbit 5 Answers
Mouse Orbit snapping issues 0 Answers