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 ?
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;)
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
Follow this Question
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