Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Zoom377 · Aug 17, 2014 at 10:05 PM · 2drotationsmooth

Mathf.LerpAngle not working after RigidBody2D collisions

Greetings,

For my own programming experience I am making a 2D Topdown space game, in which you can rotate and fly a spaceship around.

I am currently using Mathf.LerpAngle to rotate the spaceship so that is is facing towards the location of the cursor on the screen, it works mostly fine but there is a problem:

Collisions with other rigidbody2ds work fine, however after a collision that has rotated the spaceship, the spaceship will no longer point towards the cursor, but to an offset to the cursor. For example: I fly the spaceship towards an object and collide with it, and the space ship is rotated 45 degrees clockwise due the collision. Undesirably after this point, the space ship will still rotate in the desired way but to a direction 45 degrees clockwise from the direction to the cursor.

I have tried for several or more hours to find a solution, however I cannot find one. The solutions I have tried include:

-Using Mathf.Lerp(In case the problem was due to Mathf.LerpAngle)

-Altering the spaceship's rotation by directly using Quaternion.Euler(x,y,z) instead of setting rotation using transform.rotation.eulerAngles

-Using Debug.Log() to check that variables have rational values

I can only assume that perhaps using Mathf.LerpAngle in an unnatural way or there is something I have overlooked or simply do not understand about the unity engine?

When this problem occurs the values passed to Mathf.LerpAngle are not incorrect, they still are as they should be, but the value returned is not rational and the behavior of Mathf.LerpAngle seems different after a collision.

Any ideas on how to fix this?

Code:

 #pragma strict

 var rotSpeed:float;
 var mainCamera:Camera;

 private var mouseDirection:Vector2;
     
 function Start () {

 }
 
  
 function FixedUpdate () {
 
 var forward:Vector2 = Vector2(transform.up.x,transform.up.y);//Forward relative to spaceship's rotation
 var right:Vector2 = Vector2(transform.right.x, transform.right.y);//Right relative to spaceship's rotation
 
 var pixelPos:Vector3 = mainCamera.WorldToScreenPoint(transform.position);//Pixel coordinates of spaceship
 
 
 var playerPosition:Vector2 = Vector2(pixelPos.x, pixelPos.y);//Pixel coordinates of spaceship put into a Vector2
 var mousePosition:Vector2 = Vector2(Input.mousePosition.x , Input.mousePosition.y);//Pixel coordinates of the cursor
 
 
 mouseDirection = mousePosition - playerPosition;//Vector2 representing the direction from the spaceship to the cursor
 
 
 
 //A quaternion rotation that represents the rotation that must be a acheived to be facing towards the location of the cursor
 var desiredRotation = Quaternion.LookRotation(Vector3(0,0,1), Vector3(mouseDirection.x,mouseDirection.y,0));
 
 var desiredZ = desiredRotation.eulerAngles.z;//The desired rotation in degrees
 var currentZ = transform.rotation.eulerAngles.z;//The current rotation in degrees
 
 
 Debug.Log(currentZ.ToString() + "  " + desiredZ.ToString());
 
 
 transform.eulerAngles = Vector3(0,0,Mathf.LerpAngle(currentZ, desiredZ, Time.deltaTime * rotSpeed));//Smoothly lerp the rotation of the ship
 //"rotSpeed" is a float that can be changed through unity

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Zoom377 · Aug 18, 2014 at 10:53 PM

Fixed!

It turns out that I had completely forgotten about the angular velocity of the spaceship, since I had set the angular drag to 0(Was thinking: no drag in space) it would not decelerate and would fight the rotation of Mathf.LerpAngle.

I simply increased the angular drag on the RigidBody2D component of the spaceship and now all works fine.

(I feel so silly answering my own question after only 1 day...)

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Smooth 2D rotation unexpected 1 Answer

How to smoothly rotate object in direction of analog joystick? 1 Answer

How to rotate an RigidBody2D object to an angle and get back to the previous position in the smooth way? 1 Answer

Rotating a character smoothly around 90 degrees 2 Answers

How to rotate 2D object to location of joystick? 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