- Home /
The question is answered, right answer was accepted
Why is gameobject rotating by 90 degrees only once?
I have bouncing 2D bullet.
My gameObject (bullet) should rotate 90 degrees in the direction of movement after each hit.
Unfortunately, the rotation takes place only after the first hit.
Can anyone explain why this is happening and improve my script?
Rigidbody2D rb;
float constantSpeed = 10f;
// stored in fixed update, to be used in OnCollisionEnter (which may have an altered value).
Vector2 currSpeed;
// Use this for initialization
void Awake () {
rb = GetComponent<Rigidbody2D> ();
rb.velocity = new Vector2 (6f, 6f);
}
// Update is called once per frame
void FixedUpdate()
{
currSpeed = rb.velocity = constantSpeed * (rb.velocity.normalized);
}
void OnCollisionEnter2D(Collision2D collision)
{
rb.velocity = Vector2.Reflect(currSpeed, collision.contacts[0].normal);
rb.rotation = 90 ;
}
}
Answer by Gordrik · Apr 05, 2018 at 10:45 PM
it occurs every time the collision is detected, but you don't see change because your bullet actually have rotation by 90 degrees, because it's assigned, try adding the value
rb.rotation +=90;
if this doesn't work properly try:
rb.MoveRotation(Mathf.LerpAngle(rb.rotation,rb.rotation+90,1));
UPDATE For object rotation to follow its velocity you will need something like that:
angle = Mathf.Atan2(rb.velocity.y, rb.velocity.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle);
Let me know if it works, could not test it on phone.
Hi, thanks for the answer.
Unfortunately, it works strangely in my code.
When I change to:
rb.rotation +=90;
The bullet is rotating, but only in one direction.
For example: when the bullet bounces against the walls and goes to the right, it rotates 90 degrees to the right, but when it bounces to the left it also rotates 90 degrees to the right (IT SHOULD BE LEFT), which is not consistent with the direction of movement.
If I change to:
rb.$$anonymous$$oveRotation($$anonymous$$athf.LerpAngle(rb.rotation,rb.rotation+90,1));
or if I use :
Time.fixedDeltaTime
it just does not work.
Unfortunately, it doesn't work.
The link to the video shows how it looks now.
But thank you for the answer, I appreciate it :)
Oh, I'm sorry, I forgot to write that it works now.
First of all, I came back to your answer before the update.
Then I modified it a bit, also I forgot about if condition.
The working code looks like this:
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == ("Background") || collision.gameObject.tag == ("Enemy"))
{
float angle = $$anonymous$$athf.Atan2(rb.velocity.y, rb.velocity .x) * $$anonymous$$athf.Rad2Deg;
rb.rotation = angle - 90;
}
}
Thank you again for the best answer :)
Follow this Question
Related Questions
Checking GameObject Position on Andriod 1 Answer
Third Person Character Controller Movement 1 Answer
Rotation used as movement 4 Answers
Joystick for WASD & Joystick for Camera? 0 Answers
Stop camera from moving when parent rotates forward 1 Answer