Search Unity

How can i make a circular move

Discussion in 'Scripting' started by alvaromorales_unity, Aug 14, 2019.

  1. alvaromorales_unity

    alvaromorales_unity

    Joined:
    Aug 14, 2019
    Posts:
    9
    Hi! I'm really new to scripting and i really don't know anything.I'm trying to make the movements of the earth and the moon around the sun (figuring movements where circular) and i'm triying with transform.Translate but i don't know how to make it move around the sun. Thanks in advance!:)
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  3. gononono64

    gononono64

    Joined:
    Mar 1, 2015
    Posts:
    13
    Youll want to do something like this


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RotateAround : MonoBehaviour
    6. {
    7.  
    8.     public GameObject rotateAround;
    9.     public int speed;
    10.     private Vector3 axis = Vector3.up;
    11.  
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {    
    16.         transform.RotateAround(rotateAround.transform.position, axis, speed * Time.deltaTime);
    17.     }
    18. }
    19.  
    Note: you can change the axis to any other vector based on which axis you want it to orbit
     
    Last edited: Aug 14, 2019
    alvaromorales_unity likes this.
  4. alvaromorales_unity

    alvaromorales_unity

    Joined:
    Aug 14, 2019
    Posts:
    9
    What you called "rotateAround" i called "sun" but when i press play it show up a message that says the variable sun of my script has not been assigned. How do i assign the sun variable to the sun object? Probably i have like 2 IQ sorry.
     
  5. gononono64

    gononono64

    Joined:
    Mar 1, 2015
    Posts:
    13
    A public variable will show up in the inspector on the object you place the script on. Dont rename anything in the script, rather add it as a component on the object you want to orbit the sun (aka earth) and click and drag the sun object from the hierachy into the inspector (under the RotateAround component) to the GameObject field "Rotate Around"
     
    alvaromorales_unity likes this.
  6. alvaromorales_unity

    alvaromorales_unity

    Joined:
    Aug 14, 2019
    Posts:
    9
    Oh really thank you i think i got it <3
     
    gononono64 likes this.