• 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 Mattivc · Oct 12, 2010 at 03:55 PM · 2drotationquaternionaim

How to do 2D aiming

I am trying to make a 2D game, and i am currently working on a missile that smoothly aims for a another object. My current code looks like this:

Vector3 targetPosition = new Vector3(missileTarget.position.x, missileTarget.position.y, transform.position.z);

Quaternion rotation = Quaternion.LookRotation(targetPosition - transform.position, Vector3.back); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);

The problem is that Quaternion.Slerp rotates the object in all 3 rotation axis, even trough both the missile and the target is on the same 2D plane. As it is actually shorter sometimes, problem is of course that my game is a 2D game. How can i make sure the missile will be rotated in the X rotation axis only?

Comment
Add comment · Show 1
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 dmpaskiet · Oct 12, 2010 at 04:30 PM 0
Share

If you look on the script Wiki there is a script called Force2D that restricts objects to a certain axis. If you want it to be x only you may have to put the script on the object twice, once to keep it off the z axis and once to keep it off the y.

2 Replies

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

Answer by Ray-Pendergraph · Oct 12, 2010 at 04:05 PM

Use Quaternion.AngleAxis to rotate on the axes individually. Try using that, but I am not sure that this is going to solve your problem completely. Is the game actually projected onto the 2D screen plane or are all the aspects just operating at the same Z coordinate for example? If it's the former, you might get slightly incorrect results with rotations.

An example of this would be

transform.rotation = Quaternion.AngleAxis(30f,transform.up) * transform.rotation;

to take the object at its current rotation and rotate it 30 degrees around it's local up axis. You could use Vector3.up for the world up axis. To use Slerp, just store the new rotation and Slerp to it instead of setting it directly.

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
avatar image
0

Answer by cregox · Apr 11, 2012 at 02:53 PM

Just lock the other two axis. Add something like this to the end of `Update`:

 transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, 0, 0);
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Rotating an object to target on the Z axis 1 Answer

Lock Rotation of Object between 2 points, which is looking at the direction the mouse is pointing 0 Answers

Transform Z axis rotation from center of screen toward mouse cursor with Quaternion.LookRotation for a top down game. 2 Answers

Add force to player at a certain z angle in 2D 1 Answer

Turret script. Making it aim in heigth only. 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