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
0
Question by Aladine · Aug 08, 2013 at 04:59 PM · rotationtowards

"Rotate toward" in 2D

Hi,

so am trying to make a simple 2D shooter game (a clone of a flash game i made a year ago ) until now, and thanx to the unity community help, i was able to make the ship move where i click, but for the rotation am stacked again, i tried to do something like this :

 Vector3 lookDir = transform.position - targetPosition;
             lookDir.y = 0;
             transform.rotation = Quaternion.LookRotation (lookDir);


but the rotation is tottally messed up, i tried to set the x to 0 then the z to 0, but always no luck doing that

thank you

Comment
Add comment · Show 4
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 robertbu · Aug 08, 2013 at 05:09 PM 1
Share

Solving these problems depends on how you setup your model. The look functions like LooAt() and LookRotation() depend on the forward of the model being pointed at positive 'z' when the rotation is (0,0,0). It's best to fix this in a 3D modeling program, but you can get around by using an empty game object and making the visible ship a child of the empty object. The script goes on the empty game object.

avatar image boni · Aug 08, 2013 at 05:12 PM 0
Share

You could simply use transform.LookAt(targetPosition); This way it doesn't matter how your.. "gameplay"-plane is set up. (orientation, offset,...)

avatar image robertbu · Aug 08, 2013 at 05:15 PM 0
Share

@boni - it does matter. LookAt() points the positive 'Z' of the object towards the specified point. If the model does not have the front of the ship point at positive 'z', then the rotation will not be correct. His calculation does the exact same thing as LookAt().

avatar image Aladine · Aug 08, 2013 at 05:22 PM 0
Share

@robertbu - how do i know the forwoard of the model ? and where is he pointed at ? and could you please explain more about the empty object solution ? just one last thing, is there any place where developers with 2D background can start with unity3D ? cause i am really confused

1 Reply

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

Answer by robertbu · Aug 08, 2013 at 05:27 PM

Take your model in the scene, set the rotation to (0,0,0). The side facing positive 'Z' is forward. In a new scene the default camera setup looks at the back of objects in the Game view when rotation is (0,0,0). BTW your code looks fine if the model was setup correctly.

To implement the empty game object soution:

  • Place your model at (0,0,0)

  • Create an place an empty game object at (0,0,0) with rotation (0,0,0)

  • Make the model a child of the empty game object by dragging the model on top of the empty game object in the Hierarchy view.

  • Rotate the model (now a child) intil the front of the model is facing positive 'Z'.

  • Remove the script you had above from the model and place it on the empty game object.

There are other ways of setup this up and/or solving this rotation problem. Again the best way is to fix it in the modeling program.

Comment
Add comment · Show 6 · 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 Aladine · Aug 08, 2013 at 05:42 PM 0
Share

am sorry but it didn't work (i think i didn't fully understand) Question : when i start the game and let object move, then when i change the rotation of Z axis in the inspector, i got the exact result am looking at, can't i just transform that into codes ?

avatar image robertbu · Aug 08, 2013 at 05:50 PM 0
Share

First, the fix above will work. But yes you can just write code to fix it given the right information. For me to suggest a fix for your specific setup I need to know two things:

  • What is the inital rotation of your object (I assume it is a plane)?

  • What side of the plane (usually top or right), do you want to do the pointing towoards the target?

I'd like to do a quick test of any specific solution before I give it to you, and it will be a couple of hours before I'm back at a computer with Unity installed.

avatar image Aladine · Aug 08, 2013 at 06:07 PM 0
Share

the object rotation is X:-90,Y:0,Z:0 and i honestly don't know what side am using :p and yes i wanted to point towards the target (not immediately) like here it's a basic "car" movement with the mouse nothing more, if you needed more information, here is the whole project here it's less than 1mo, i'll be very happy if you fix it there with some comments in the scripts, cause finding some good 2D reference for unity is pretty hard (maybe am looking at it from the wrong side) thank you very much

avatar image robertbu · Aug 08, 2013 at 08:54 PM 0
Share

You will find a package with two solutions to your problem here. The first solution is the child game object as outlined above. For the second solution:

  • I used the CreatePlane editor script from the Wiki to create a new plane that faces the camera (vertical) when the rotation is (0,0,0).

  • I made a new material with the arrow facing right when the rotation is (0,0,0).

  • I duplicated your script and change the rotation code for this new plane. The new rotation for this plane is:

           if (targetPosition != transform.position) {
                 Vector3 lookPos = targetPosition - transform.position;
                 float angle = $$anonymous$$athf.Atan2(lookPos.y, lookPos.x) * $$anonymous$$athf.Rad2Deg;
                 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
             }
    
    
avatar image Aladine · Aug 08, 2013 at 10:09 PM 0
Share

Thank you very much !!! this is really helpful !! there is a little bug when the cursor is over the plan, it rotates completely wrong but i'll try to find a way to fix that, thank you again !! EDIT: the bug i talk about exist only in my scene, the "CreatePlan" and "child" solutions works perfectly fine again thank you so much

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.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Move object A towards object B 2 Answers

How to make an object rotate on the y axis towards the mouse? 3 Answers

2D Rotation of a Sprite - Quaternion.FromToRotation Smooth 1 Answer

Torque towards Quaternion. 0 Answers


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