Rigidbody2D Flight
Hello all,
Been working on this little project with a Rigidbody2D rocket, however, the flight of the rocket is not working quite how I would like it to. I would like the rocket to always move in the direction it is facing, like a jet. As it is now, it kind of Tokyo drifts round corners. This video maybe explains it better:
https://www.youtube.com/watch?v=lJxldkuWuV8
If anyone could shed some light on how to code this I would be most grateful!
Also see images for what I am trying to achieve.
Many thanks in advance!
public float velocity;
public float leftRotationSpeed;
public float rightRotationSpeed;
void FixedUpdate()
{
if (Input.GetKey (KeyCode.UpArrow))
{
GetComponent<Rigidbody2D>().AddForce(transform.up * velocity);
}
if (Input.GetKey (KeyCode.DownArrow))
{
GetComponent<Rigidbody2D>().AddForce(transform.up * -velocity);
}
if (Input.GetKey (KeyCode.LeftArrow))
{
GetComponent<Rigidbody2D>().AddTorque(Input.GetAxis ("Horizontal") * leftRotationSpeed);
}
if (Input.GetKey (KeyCode.RightArrow))
{
GetComponent<Rigidbody2D>().AddTorque(Input.GetAxis ("Horizontal") * rightRotationSpeed);
}
}
}![alt text][1]
[1]: /storage/temp/58457-rocket2.jpg
Answer by Dinosaurs · Nov 19, 2015 at 10:14 PM
Rather than adding torque, it sounds like you should just rotate the object. That will make the rotation instant rather than slowly building force.
http://docs.unity3d.com/ScriptReference/Rigidbody2D.MoveRotation.html
Also, you should store the Rigidbody2D in a variable rather than calling GetComponent() every FixedUpdate(). Repeated calls to GetComponent are very expensive and will likely cause performance issues down the line.
Answer by Ozwig · Nov 19, 2015 at 10:21 PM
Thanks @Dinosaurs! Though I do quite like the slight physics lag you get with torque. Do you think I can achieve the same effect with .MoveRotation?
Yes, there are many ways you could achieve that effect. One way might be to store the current rotation speed in a variable and use $$anonymous$$athf.Lerp() to slowly increase it to a maximum rotation speed before applying the rotation.
In the future if you want to follow up on an answer, please leave a comment rather than creating a new answer. And if an answer solves your issue, press the checkmark to accept it so others know it's a working solution.
Thanks again. I have now changed from AddTorque to $$anonymous$$oveRotation and the effect is the same (the rocket travels sideways round corners due to its momentum) but without the lag during rotation. Can you think of any other way I could make it appear to s$$anonymous$$r around obstacles?
$$anonymous$$any thanks for your help.
Rather than adding force, set the velocity to the forward vector.
Only just got round to this now. Setting the Velocity worked perfectly. $$anonymous$$any Thanks @Dinosaurs!
Your answer
Follow this Question
Related Questions
Using rb.velocity and Add Force on the same gameobject? 0 Answers
Instantiated Prefab no Rigidbody2D on Update function 1 Answer
Shooting a bullet that stops in place when mouse is released 0 Answers
,Doesn't stop adding force to enemy 0 Answers
2D : How to overwrite rigidbody velocity when "skill" input pressed ??? 0 Answers