Jump to content
  • Advertisement
Sign in to follow this  
barraghah

Initial positions for orbiting satellites

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)

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

Edited by Alberth

Share this post


Link to post
Share on other sites

Thanks Alberth for the simple solution!

So I was obviously greatly overcomplicating the issue.

 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!