• 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
0
Question by Robs · Sep 24, 2014 at 06:19 AM · 2drotation2d gamequaternion

How do I rotate a 2D object based on it's velocity?

Hello everyone,

I am working on a 2D game and I am trying to get my character to rotate based on its velocity. An example of the effect that I am trying to get can be seen in this video: http://youtu.be/mYUzCq6G0AY?t=25s

Quaternions and rotation in general are a bit over my head, and trying to map rotation to the characters velocity makes it even more difficult. Can someone help me out?

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
2
Best Answer

Answer by robertbu · Sep 24, 2014 at 11:39 AM

The following code assumes that the front side of your sprite is pointing right when the rotation is (0,0,0).

 function FixedUpdate() {
 var dir = rigidbody2D.velocity;
 var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
 rigidbody2D.MoveRotation(angle);
 }

No Quaternions involved :)

Comment
Add comment · Show 9 · 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 Robs · Sep 24, 2014 at 06:08 PM 0
Share

I dropped that code in and the sprite just alternates between pointing straight up (90 degrees) when moving up on the screen, and straight down (-90 degrees) when moving down on the screen. If I stop giving input, the sprite eventually points forward again.

Here is the code that I am using to control the player. I think that maybe the problem is here, but I don't know. Any help would be greatly appreciated.

 void Update () {
     
     // I'm using an on-screen UI slider to simulate the player sliding their finger up and down on the screen.
     // The slider goes from 0.0f - 1.0f. I am setting it to 0.5f by default (center)
     if (Input.Get$$anonymous$$ey("up") || sliderValue > 0.6f){
         move = true;
         force = new Vector2 (0.0f, moveForce * $$anonymous$$athf.Sign(sliderValue)); // moveForce is 20.0f
     }
     
     if (Input.Get$$anonymous$$ey("down") || sliderValue < 0.4f){
         move = true;
         force = new Vector2 (0.0f, -moveForce * $$anonymous$$athf.Sign(sliderValue));
     }
     print (sliderValue);
 }
 
 void FixedUpdate(){
     if (move) {
         if (fuel > 0.0f) {
             rigidbody2D.AddForce(force);
         }
         move = false;
     }
 }
 

avatar image robertbu · Sep 24, 2014 at 07:09 PM 0
Share

I tested the code above before I posted it. It assumes 1) it is the only thing impacting the rotation, that the ship is a sprite, and that the 'front' is facing right when the sprite's rotation is (0,0,0). If you want to put a package of your project (or an example project demonstrating the problem), on the net and provide a link, I'll download it and figure out what is going on.

avatar image Robs · Sep 26, 2014 at 07:49 PM 0
Share

The project files are located below. I am using Unity 4.6 beta 18 for the UI. If you aren't running 4.6 just let me know and I can package it back up without the UI element. Thank you very much for looking at this for me!

http://ge.tt/68mYRhz1/v/0?c

avatar image robertbu · Sep 26, 2014 at 10:09 PM 0
Share

The rotation code I provided uses the velocity of the player to calculation the direction. I'm going to assume that you plan to keep the ship in one place on the 'x' axis and move the obstacles to the ship. If that is the case, then you need to add in the relative world velocity of the ship into 'dir' before doing the calculation. Here are some changes to your code to demonstrate. You'll need to adjust the 'worldVelocity' to match the rate at which obstacles move towards the player.

 using UnityEngine;
 using System.Collections;
 
 public class Player : $$anonymous$$onoBehaviour {
     
     float maxSpeedY = 10.0f;
 
     Vector2 worldVelocity = Vector2.right * 2.5f;
     
     void Start(){
         Physics2D.gravity = new Vector2 (0.0f, 0.0f);
     
     }
 
     void FixedUpdate () {
 
         var dir = rigidbody2D.velocity;
         dir += worldVelocity;
         var angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
         rigidbody2D.$$anonymous$$oveRotation(angle);
 
         if ($$anonymous$$athf.Abs(rigidbody2D.velocity.y) >= maxSpeedY){
             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, $$anonymous$$athf.Sign(rigidbody2D.velocity.y) * maxSpeedY);
         }        
     }
 }

 
avatar image Robs · Sep 26, 2014 at 10:28 PM 0
Share

That worked! Thank you so much! One other question, is there a way to limit how much the object rotates? For example, if I wanted to make sure that it never passes +45 or -45 degrees.

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Counting a 2D objects flips, full 360 rotation on the Z axis. C# 0 Answers

How do I get the sprite pointing the right direction? 1 Answer

What causes this object to re-rotate itself after it hits it's destination? 1 Answer

Tracking the object in the 2D game 0 Answers

2D Zig-zag movement and rotation 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges