- Home /
Unity2D move + turn with a fixed angle
Hello,
i have a question about moving a sprite object.
I'm using a rigidbody2D and i want a constant speed.
So i used rigidbody2D.velocity ( Maybe .AddForce is better but i want that the speed never fade away. And with Force it does :(.)
So when i press the left/right key the object should make a curve by a fixed angle.
http://curvefever.com/play2.php ( Just click play ) Like this game >.<
Maybe someone can help me.
Regards
LifeArtist
Without your code, I cannot give focused, accurate answer. I'm guessing is that you need to add your force relative to the direction the object is facing ins$$anonymous$$d of a world direction. For example, if you are using Vector3.forward, change to using Transform.forward.
Sry ... i didnt saw ur comment -.-"
if (Input.Get$$anonymous$$ey(moveRight))
{
rigidbody2D.transform.Rotate(0F, 0F, 7F, Space.Self);
}
if (Input.Get$$anonymous$$ey(moveLeft))
{
rigidbody2D.AddForce(new Vector2(0F, 5F));
}
I use Vector2 ... and my object just rotates and still moving up and down.
Answer by robertbu · Mar 17, 2014 at 07:55 PM
I'm assuming the 'forward' of your ship is the 'up' direction when the rotation is (0,0,0). Your code should be executed in FixedUpdate(). Try this to address your problem:
void FixedUpdate() {
if (Input.GetKey(moveRight))
{
transform.Rotate(0F, 0F, 7F);
}
if (Input.GetKey(moveLeft))
{
rigidbody2D.AddForce(transform.up * 5F);
}
}
Your answer
Follow this Question
Related Questions
Move Object With Angle In 2D 1 Answer
2D Movement Problems 2 Answers
2D Animation does not start 1 Answer
2D Character grabing the wall 1 Answer
How to change angle-roll object with mobile input? 0 Answers