Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 28, 2018 at 06:25 PM by polan31 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by polan31 · Apr 05, 2018 at 09:11 PM · rotationmovementscript.bounce

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 ;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image polan31 · Apr 06, 2018 at 08:30 AM 0
Share

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.

avatar image polan31 · Apr 08, 2018 at 03:01 PM 0
Share

Unfortunately, it doesn't work.

The link to the video shows how it looks now.

link text

But thank you for the answer, I appreciate it :)

avatar image Gordrik · Apr 09, 2018 at 08:21 AM 0
Share

Could you update the code? I will try to troubleshoot

avatar image polan31 Gordrik · Apr 09, 2018 at 08:50 AM 0
Share

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

Answers Answers and Comments

142 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges