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 /
avatar image
0
Question by bruceevans · May 12, 2015 at 10:01 PM · rotationvector3gravityforces

How to rotate a ship based on gravity around a planet?

Hey everyone, here's a youtube link to my problem. I'm trying to get my ship to rotate with the gravity pull so that it can go into a more orbit-like motion. If I need to supply more info please let me know. Here are a couple of the scripts on my ship with a couple of my ideas commented out.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControllerForce : MonoBehaviour {
 
     public float speed;
     public float maxSpeed;
     //public static float angleFacing;
     //public Vector3[] positions = new Vector3[2];
 
     //private Vector3 lastPosition;
     private Rigidbody playerRB;
     //private int frameNumber = 0;
     //private  positions : LinkedList.<Vector3> = new LinkedList.<Vector3>();
 
     // Use this for initialization
     void Start () {
         playerRB = GetComponent<Rigidbody> ();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
 
         //forward movement and limiting top speed
         playerRB.AddForce (transform.forward * speed);
 
         if (playerRB.velocity.magnitude > maxSpeed) {
             playerRB.velocity = playerRB.velocity.normalized * maxSpeed;//returning a vector with the value of 1 and multiplying by maxSpeed
         }
 
         //Vector3 currentPosition = playerRB.transform.position;
 
         //if (frameNumber % 2 == 0) {
         //    lastPosition = playerRB.transform.position;
         //}
 
         //for (int i = 0; i <= 1; i++) {
         //    positions[i] = playerRB.transform.position;
         //}
 
         //Debug.Log ("lp is " + lastPosition);
         //Debug.Log ("p is " + playerRB.transform.position);
 
 
         //frameNumber ++;
         //angleFacing = Mathf.Atan2 ((playerRB.transform.position.z - lastPosition.z), playerRB.transform.position.x - lastPosition.x);
 
         //if (angleFacing != 0) {
         //    playerRB.rotation = Quaternion.Euler (0.0f, angleFacing + 90, 0.0f);
         //}
 
         //Debug.Log ("angle is " + angleFacing);
     }
 }



AND this one for all of my user input and rotation.


 using UnityEngine;
 using System.Collections;
 
 public static class ExtensionMethods {
     public static float Remap (this float value, float from1, float to1, float from2, float to2) {
         return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
     }
 }
 
 public class Rotate_Script : MonoBehaviour {
 
     public static float direction = 90.0f;
     public float countdown = 3.0f;
 
     public GameObject scaleGUI; //rotation cube GUI
 
     private Rigidbody playerRB;
     private float scaleRot = 1.0f; //value holder for scale ratio
     private bool empty = false;
 
 
 
     // Use this for initialization
     void Start () {
         playerRB = GetComponent<Rigidbody> ();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         if (scaleRot > 0 && empty == false){
             //Angle adjustments
             if (Input.GetKey ("up") && countdown > 0.0f) {
                 direction -= 2.0f;
                 countdown -= Time.deltaTime;
                 scaleRot = countdown.Remap (0.0f, 3.0f, 0.0f, 1.0f);
                 scaleGUI.transform.localScale = new Vector3 (1.0f, scaleRot, 1.0f);
             
             } else if (Input.GetKey ("down") && countdown > 0.0f) {
                 direction += 2.0f;
                 countdown -= Time.deltaTime;
                 scaleRot = countdown.Remap (0.0f, 3.0f, 0.0f, 1.0f);
                 scaleGUI.transform.localScale = new Vector3 (1.0f, scaleRot, 1.0f);
             }
         } else {
             empty = true;
             scaleRot += 0.1f * Time.deltaTime;
             scaleGUI.transform.localScale = new Vector3 (1.0f, scaleRot, 1.0f);
         }
 
 
         //GUI Code maybe move this later
         if (scaleRot >= 1.0f) {
             countdown = 3.0f;
             empty = false;
         }
         
         playerRB.rotation = Quaternion.Euler (0.0f, direction, 0.0f);
 
 
 
         //Debug.Log (oldPosition.x);
         //Debug.Log (playerRB.position.x);
     }
 }


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

Answer by AlwaysSunny · May 12, 2015 at 10:15 PM

This is not a physically sound way to cap a velocity (following your video). The "best" way involves a creating a "soft" cap that limits forces that would result in exceeding a soft threshold. A hard threshold can create weirdness, but may serve as a (potentially problematic) insurance policy against crazy big forces.

Generally any time you write over (as distinct from "change", which is better but still affects physics purity) a body's velocity, you should not expect it to behave accurately in all conditions or play well with others except in very special cases. You can use the Dot Product of (velocity DOT requested_acceleration) to measure whether adding a force will exceed a soft threshold.

Regarding getting an orbit thing going, you should be aware that creating a stable orbit involves some rather complex thought. From what I'm seeing about the game and your request, I think your best bet is to fudge it. As opposed to using the gravitational formula and finding the proper angle of entry, etc, etc. That's literally rocket science.

Given a body at rest, there are two helpful formulas I know. One is the famous gravitational formula of classical physics, and the other is the force to apply to achieve a stable orbit. I do not know how to do this when the body in question is a vehicle capable of moving under its own power; that introduces a whole new set of problems with which I'm not familiar.

Think game science! How can you turn what you know into what you need in the simplest terms? You do not appear to need a true gravitational simulation to achieve orbit or "orbit like" behavior. A nearby "heavy" object might affect a lighter body like your ship by looking at whether a clockwise or counter-clockwise path would produce movement around the heavy body, and applying some force, scaled by relative distance, to achieve that kind of motion. It's definitely an "arcade style" physics approach. This is just a thought, though. There is surely a nice tidy way to achieve this. You've picked an deceptively more difficult task than you realize.

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

Applying force with respect to angle with the ground? 1 Answer

Possible to create axis centered gravity? 1 Answer

Tornado Simulator 0 Answers

0-360 Y degree from Vector3.Angle 1 Answer

How can I rotate and move a cylindrical rod from one side with respect to other side? 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