Question by
eporetsky · Mar 07, 2016 at 11:14 AM ·
2d-physicsaddrelativeforcejetpackspacegame
Unity2d AddRelativeForce Behaves Randomly
So I'm building a 2d physics jetgame. The game starts without spaceship spinning around a sun like this:
void OnTriggerStay2D(Collider2D other) { player = other.GetComponent();
//Calculate the vector that points from Player to Sun
Vector2 relativePos = (dev.position - rb.position);
//Based on the relative vector, this angle makes sure that Player is perpendicular to the vector
float angle = Mathf.Atan2(relativePos.y, relativePos.x) * Mathf.Rad2Deg;
//This is the direction to which Player should rotate towards
Quaternion rotate = Quaternion.AngleAxis(Mathf.Round(angle)+ 2.6f, Vector3.forward);
Quaternion current = player.transform.rotation;
//Rotates Player, and moves his position "upwards" relative to itself
player.transform.rotation = Quaternion.RotateTowards(current, rotate, Time.deltaTime * 50000);
player.transform.Translate(Vector3.up * Time.deltaTime * 5);
}
in order to simulate the jetpack im then doing this code
void FixedUpdate()
{
if (Input.GetKeyDown("space"))
{
// remove the sun collider when dev lifts off
GameObject sun = GameObject.FindWithTag ("Sun");
CircleCollider2D sColl = sun.GetComponent<CircleCollider2D> ();
sColl.enabled = false;
if (jetCount < 3) {
rb.AddRelativeForce (Vector2.up * jetpackAccel);
jetCount++;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612070857im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How Do I make my character move? 0 Answers
C# 2D top down shooter game, How do I get the player to shoot towards the mouse cursor? 0 Answers
How to move rigidbodies with 2D Character controller (No rigidbody) 1 Answer
in a 2d game do i use rigidbody or rigidbody 2d? etc 2 Answers
Unity 2D physics: problem about gravity simulation 0 Answers