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

Predicted Trajectory path in Unity3d with gravity

Discussion in 'Physics' started by BluEternity, Jul 31, 2021.

  1. BluEternity

    BluEternity

    Joined:
    Jul 29, 2021
    Posts:
    4
    hello, i am a beginner to unity and was wondering how i could project a trajectory path of an object. i want to be able to see the path taken by the object as i slowly increase the velocity so as to reach an orbit of some kind.
    i was wanting to make a trajectory path script like sebastian lague's one in his video about making a solar system.
    upload_2021-7-31_19-12-43.png upload_2021-7-31_19-12-58.png
    upload_2021-7-31_19-13-13.png
    Ive tried to look at his project files but i think these seem to be implemented in a shader of some sort and i dont have the slightest clue on how they work.
    Thanks in advance!
    Edit: i want to know the trajectory of the planet before actually releasing it.
     
    Last edited: Jul 31, 2021
  2. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    Hi!
    You have the script and the video to watch the parameters
     
  3. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21

    with a circular path BEZIER Curve
     
  4. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    Orbit path with math .............
     
  5. BluEternity

    BluEternity

    Joined:
    Jul 29, 2021
    Posts:
    4
    Thank you so much! i may try to implement this method to tackle orbital mechanics.
     
  6. BluEternity

    BluEternity

    Joined:
    Jul 29, 2021
    Posts:
    4
    thanks for the advice, i think this could work better in another game. Thanks anyways!
     
  7. BluEternity

    BluEternity

    Joined:
    Jul 29, 2021
    Posts:
    4
    hmmm i would like it if i knew the trajectory of the object before it's released
     
    Last edited: Jul 31, 2021
  8. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    Hi! i have a problem, the spehre don't stop when touch the Sun ?
    Can you help me please, thanks in advance.






    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Oribiter : MonoBehaviour
    {
    // Start is called before the first frame update
    public int sphereCount = 1000;
    public int maxRadius = 200;
    public GameObject [] spheres;
    public Material [] mats;
    public Material trailMat;
    void awake()
    {
    spheres = new GameObject [sphereCount];


    }
    void Start()
    {

    spheres = CreateSpheres(sphereCount,maxRadius);


    }
    public GameObject[] CreateSpheres(int count, int radius)
    // Update is called once per frame
    {var sphs = new GameObject [count];
    var sphereToCopy = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    Rigidbody rb = sphereToCopy.AddComponent<Rigidbody>();
    rb.useGravity = false;
    for (int i = 0; i < count ; i++)
    {
    var sp= GameObject.Instantiate(sphereToCopy);
    sp.transform.position = this.transform.position+
    new Vector3(Random.Range(-maxRadius,maxRadius),
    Random.Range(-10,10),
    Random.Range(-maxRadius,maxRadius));
    sp.transform.localScale *= Random.Range(0.5f,1);
    sp.GetComponent<Renderer>().material = mats[Random.Range(0,mats.Length)];
    TrailRenderer tr = sp.AddComponent<TrailRenderer>();
    tr.time = 1.0f;
    tr.startWidth = 0.1f;
    tr.endWidth = 0;
    tr.material = trailMat;
    tr.startColor = new Color (1,1,0,0.1f);
    tr.endColor = new Color (0,0,0,0);
    spheres = sp;

    }
    GameObject.Destroy(sphereToCopy);
    return spheres;

    }
    void Update(){
    foreach (GameObject s in spheres)
    {
    Vector3 difference = this.transform.position - s.transform.position;
    float dist = difference.magnitude;
    Vector3 gravityDirection = difference.normalized;
    float gravity = 6.7f * (this.transform.localScale.x * s.transform.localScale.x * 80)/ (dist * dist);
    Vector3 gravityVector =(gravityDirection * gravity);
    s.transform.GetComponent<Rigidbody>().AddForce(s.transform.forward, ForceMode.Acceleration);
    s.transform.GetComponent<Rigidbody>().AddForce(gravityVector, ForceMode.Acceleration);
    }
    }
    }
     

    Attached Files:

  9. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    my screen with parameters.
     

    Attached Files: