Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by iubisoft · Mar 25, 2016 at 10:10 PM · c#rotationplanetship

Check the entry point of a ship in planet's orbit and tell if movement is in rotation direction (2D)

I have a planet and a ship that orbits the planet. I have to check if the ship is moving in the same direction as the planet ships. The planet has a random rotation speed and the ship can also move with different speeds. How do i do that ?

Comment
Add comment · Show 2
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
avatar image Downstream · Mar 25, 2016 at 10:16 PM 0
Share

@iubisof 2D or 3D?

avatar image iubisoft Downstream · Mar 25, 2016 at 10:17 PM 0
Share

2D, thanks for the reply

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Downstream · Mar 25, 2016 at 11:53 PM

 /*@iubisoft Basically you have to calculate the angular velocity of the planet and the angular velocity of the ship in relation to the planet. Here's my test script which I used to calculate it. It's signless. I can't think of a way to resolve flying in the opposite direction at the correct velocity this tired so I'll leave that up to you (maybe tomorrow :P):*/
 
 using UnityEngine;
 using System.Collections;
 
 public class ResolveSpeedenfp : MonoBehaviour {
     public PlanetScript planet;    // Planet is a simple script which rotates a gameObject by speed * Time.deltaTime
     public Rigidbody2D ship;        // Ship is a controllable rigidbody.
 
     void Start(){
         // This script was attached to a trigger gameObject with a parent gameObject holding the planet component.
         planet = transform.root.GetComponent<PlanetScript> ();
     }
     public void OnTriggerEnter2D(Collider2D col){
         Debug.Log ("Collision enter");
         // I was having some difficulty with getting this function to work, you might as well ignore all this :P
         try{
             this.ship = col.transform.gameObject.GetComponent<Rigidbody2D>();
         }catch{
 
         }
     }
     Vector2 enfp;    // Extrapolated next frame position.
     Vector2    prp;    // Position in reference to planet.
 
     // Update is called once per frame
     void Update () {
         if (ship != null) {
             // Additional gravity for testing purposes
             //ship.AddForce((planet.transform.position - ship.transform.position).normalized * 2f * Time.deltaTime);
 
             // This vector "extends" from the planet to the ship.
             prp = ship.transform.position - planet.transform.position;
             // This vector is the same, except we've added one frame's movement to it, extrapolated.
             enfp = prp + (ship.velocity * Time.deltaTime);
 
             // The angle between current position and next frame position should be a sufficient approximation of angular velocity.
             float angVship = Vector2.Angle (prp.normalized, enfp.normalized);
 
             // Angular velocity of the planet.
             float angV = planet.speed * Time.deltaTime;
 
             // The output of this debug log was quite convincing when the speed of the ship began to match the speed of the surface of the planet.
             Debug.Log (angV.ToString () + "   " + angVship.ToString ());
         } else {
             enfp = Vector2.zero;
             prp = enfp;
         }
     }
 }
 
 
 //Angular velocity is usually expressed in radians, but I'm using degrees/frame. (float angV = planet.speed * Time.deltaTime;)
Comment
Add comment · Show 1 · 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
avatar image iubisoft · Mar 26, 2016 at 02:48 AM 0
Share

Thank you sir. You code give me an idea on how to do it. I've succeeded. Vector3 rotationLast; Vector3 rotationDelta;

void Start(){ rotationLast = transform.rotation.eulerAngles; }

void Update(){ rotationDelta = transform.rotation.eulerAngles - rotationLast; rotationLast = transform.rotation.eulerAngles; }

This is what i used in both scripts and . In Ship i made another variable to stop the rotationDelta when is not 0. Then in OnTriggerStay2D i compare those 2 delta values from both objects. If they are both positive or negative it means they rotate in the same direction. In OnTriggerEnter i set a boolean to make sure the first entrance is in the right direction. I use this one in OnTriggerStay to decide to either call or not the function to add speed. Thank you have a nice day.

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

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate an object within a limit 0 Answers

How can i rotate an object using LookAt with only 1 axis? 0 Answers

Know when the object is rotated? 1 Answer

Object rotates normaly while moving, then does zig zags? 0 Answers

How to move an object on a 2d plane forward, backward, and in a rotation about the mouse position? 0 Answers


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