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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D planet orbit

Discussion in '2D' started by calebbabin, Mar 17, 2015.

  1. calebbabin

    calebbabin

    Joined:
    Mar 15, 2015
    Posts:
    5
    Im trying to make a game which contains multiple planets/moons orbiting around each other in 2D but I must not be doing it right because I keep running into weird problems.

    Im new to unity and I have no idea on how to use the libraries and stuff.

    anyways Im using this script to rotate a sphere to face another sphere and applying a constant 2d force of -10 on the Y axis.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class faceObject : MonoBehaviour {
    5.    
    6.     public Transform Target;
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.         Vector3 LookAtPoint = new Vector3(Target.transform.position.x, Target.transform.position.y, Target.transform.position.z);
    15.         transform.LookAt(LookAtPoint);
    16.     }
    17. }
    18.  
    And I get the following result, the sphere orbits correctly at first but then suddenly stops orbiting and crashes into the bigger orb.

    I am probably completely doing this wrong and I should probably use an entirely different method, I'm open to suggestions.

    (I have searched the internet pretty thoroughly but I could not figure out my problem by myself)
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Perhaps you should also post the code where you are adding that constant force in the Y direction.
     
  3. calebbabin

    calebbabin

    Joined:
    Mar 15, 2015
    Posts:
    5
    Its just a constant force 2d component