Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity2D Rotation&Orbit Script

Discussion in 'Scripting' started by Jakub_Szubzda, Nov 28, 2020.

  1. Jakub_Szubzda

    Jakub_Szubzda

    Joined:
    May 30, 2019
    Posts:
    2
    Hi, so I created this script that makes your object orbit other object and rotate. It is centered on transform so u can walk with it and so. Since I couldn't really find solution like this online, i'll post my so you can use it in your project, or improve it. Also, write what you think of it :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteInEditMode]
    6. public class Rotations : MonoBehaviour
    7. {
    8.     [Header("Orbit")]
    9.     public bool orbit = true;
    10.     public Transform centre;
    11.     public float radius;
    12.     public float orbitSpeed = 15f;
    13.     public float angle;
    14.     float radian;
    15.  
    16.     [Header("Rotate")]
    17.     public bool rotate = true;
    18.     public bool syncRotations = true;
    19.     public bool syncSpeeds = true;
    20.     public float rotationSpeed = 15f; // looks the best if is the same as orbitSpeed
    21.     public float angleRotation;
    22.  
    23.     private void Update()
    24.     {
    25.         if (syncSpeeds) rotationSpeed = orbitSpeed; // Sync Speeds
    26.         if (syncRotations && rotate && orbit) angleRotation = angle; // Sync Rotations
    27.  
    28.         if (Application.isPlaying) // check if script is running in editor mode or in game, if in game rotate object
    29.         {
    30.             if(orbit)  angle += orbitSpeed * Time.deltaTime;
    31.             else if(rotate)  angleRotation += rotationSpeed * Time.deltaTime;
    32.         }
    33.         checkAngles();
    34.  
    35.         radian = angle * (Mathf.PI / 180); // convert degrees to radians
    36.  
    37.         float x = (Mathf.Cos(radian) * radius) + centre.position.x; // calculate x position of orbiting object
    38.         float y = (Mathf.Sin(radian) * radius) + centre.position.y; // calculate y position of orbiting object
    39.  
    40.         transform.position = new Vector2(x, y); // apply new position to orbiting object
    41.  
    42.         transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, angleRotation); // apply new rotation to rotating object
    43.     }
    44.  
    45.     void checkAngles()
    46.     {
    47.         if (angle >= 360) angle = 0; // keep angle in -360/360 range
    48.         else if (angle <= -360) angle = 0;
    49.  
    50.         if (angleRotation >= 360) angleRotation = 0; // keep angle in -360/360 range
    51.         else if (angleRotation <= -360) angleRotation = 0;
    52.  
    53.     }
    54. }
    55.  
     

    Attached Files:

  2. frappastudio

    frappastudio

    Joined:
    Aug 29, 2014
    Posts:
    6
    Great thank you ! You can also parent your planet Transform to the star at the correct distance and just vary the Y rotation value of the star if you are really lazy, that's whay I did...