Advertisement

Initial positions for orbiting satellites

Started by March 05, 2019 07:45 PM
2 comments, last by barraghah 5 years, 6 months ago

Hello

I wonder if someone could point me to the right direction.

I have the following script, which I use to add objects as orbiting satellites for a host object:


using UnityEngine;

public class Satellite : MonoBehaviour
{

    public GameObject host;
    public float orbitSpeed = 5f;
    public float radius = 0.1f;

    private Vector3 center;
    private float angle;

    private void Start()
    {
        center = new Vector3(host.transform.position.x, host.transform.position.y, 0);
    }

    private void Update()
    {

        angle -= orbitSpeed / radius * Time.deltaTime;

        var offset = new Vector3(Mathf.Sin(angle) * radius, Mathf.Cos(angle) * radius, 0);
        transform.position = center + offset;
    }
}

Now when I create these object, I randomize the radius and it works fine. However, their initial positions are always like this:

vrgQ6K6.png

 

How could I set their initial position to be something else? Any help would be appreciated.

- Barra

Randomize an initial angle, and compute the initial x & y coordinate from it.

Advertisement

Thanks Alberth for the simple solution!

So I was obviously greatly overcomplicating the issue.

 

This topic is closed to new replies.

Advertisement