Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by aandreyev · Jan 30, 2015 at 12:10 PM · positiongravitypathspace

Predicting path line affected by planet gravity.

Hey guys,so I have a question for you.

First of all, I should describe a main concept. It's pretty simple, if you played Angry Birds space, it pretty obvious. I need make a prediction path that interact with colliders or/and with planets gravity. I have several types of planets, ones that attract objects, and the others that repel them. I have several script for all of this. 1.Scripts for planets repel/attract 2.Script for predicting path 3.Script for instantiation and slingshot. So, I know how to predict path with normal, static gravity like -9.8f for cases in your scene. But what about if you have a planet, and each object that trigger the collider of planet(kinda atmosphere) start being attracted to this planet, with acceleration, radial velocity etc. When object is out of triggers there is no any gravity at all, so predicting path line will remain straight line, not a bid deal. Her's some of my scripts.


Planet attraction:

  private float gravityFactor = 1.0f;
      private bool tri = false;
      GameObject planet;
      void Start () {
      }
      public void OnTriggerEnter(Collider collision) {
          planet = collision.gameObject;
          if(collision.CompareTag("star")) {
              tri = !tri;
          }
      }
      public void OnTriggerExit(Collider collision) {
              tri = !tri;
      }
      void FixedUpdate () {
          if(GameObject.Find("planet(Clone)")

!= null) { if( tri == true){ Vector3 pos = ((transform.position - planet.gameObject.transform.position).normalized rigidbody.mass gravityFactor / (transform.position - planet.gameObject.transform.position).sqrMagnitude);

              planet.gameObject.rigidbody.AddForce(pos,

ForceMode.VelocityChange);

          }
          }
      }



And prediction path script:

      private Vector3 StartVelocity = new Vector3(-5, 5, 0);
      public float PredictionTime;
      private Vector3 G;
      Vector3 scale = new Vector3 (1.5f,1.5f,1.5f);
      float scaleMult = 1f;
      int numb = 1;
      private bool tri = false;
      private GameObject star;
      private float gravityFactor = 1.0f;
      GameObject[] pnts  = new GameObject[60];
      void Awake() {
          Vector3 momentum = StartVelocity;
          Vector3 pos = gameObject.transform.position;
          Vector3 last = gameObject.transform.position;
      }
      void Start() {
          if(G == Vector3.zero) {
              //a hacky way of making user this gets initialized in editor too...
              //this assumes 60 samles / sec
              G = new Vector3(0, -9.8f, 0) / (PredictionTime * 60f);
          }
      }
      public void OnTriggerEnter(Collider collision) {
          star = collision.gameObject;
          if(collision.CompareTag("star")) {
              tri = !tri;
          }
          foreach(GameObject sph in pnts) {
              if(tri){
                  G = ((star.gameObject.transform.position -

sph.gameObject.transform.position).normalized rigidbody.mass gravityFactor / (star.gameObject.transform.position - sph.gameObject.transform.position).sqrMagnitude); sph.gameObject.transform.position += G; } }

      }
      public void OnTriggerExit(Collider collision) {
              tri = !tri;
      }
      void Update() {
          if(Input.GetMouseButtonDown(0)) {
              Vector3 momentum = StartVelocity;
              Vector3 pos = gameObject.transform.position;
              Vector3 last = gameObject.transform.position;
                  for(int i = 0; i< (int) (PredictionTime * 15); i++) {
                      momentum += G;
                      pos += momentum;
                      GameObject sph = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                      sph.gameObject.transform.position = new Vector3(last.x, last.y, 0);
                      sph.collider.isTrigger = true;
                      sph.transform.localScale = scale;
                      sph.gameObject.name = numb.ToString();
                      scale *= scaleMult;
                      scaleMult-= 0.005f;
                      last = pos;
                      Debug.Log (numb);
                      numb+=1;
                      pnts[i] = sph;
                      if(tri == true) {
                          G = ((star.gameObject.transform.position -

sph.gameObject.transform.position).normalized rigidbody.mass gravityFactor / (star.gameObject.transform.position - sph.gameObject.transform.position).sqrMagnitude); momentum +=G; pos += momentum; sph.gameObject.transform.position = last; last = pos; }

                  }
          }
          if(Input.GetMouseButtonUp(0)) {
              for(int i = 0; i< (int) (PredictionTime * 30); i++) {
                  Destroy (GameObject.Find(numb.ToString() +

"(Clone)")); numb-=1; } } }


So, what I should do with predicting path line so it cab be affected by planets gravity. Thanks for your responses, guys.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Scribe · Jan 30, 2015 at 12:13 PM

Check the edit I made to my answer HERE

You can produce effects such as:

alt text

Though it assumes your gravitational objects are stationary and will not effect each other. Also look in the comments for ways to optimise it to work better at run-time over several frames.

Hope that helps,

Scribe

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rigidbody gravity between planets 1 Answer

gravity causes position.y fluxuation sitting still 0 Answers

Moving selected unit to location in space rts based on screen touch 3 Answers

Trying to rotate player, how? 0 Answers

Drift Rotation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges