• 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
2
Question by JtheSpaceC · Oct 09, 2014 at 07:13 PM · 2dspriterotatelook

2D lock x,y rotation to look at target

Hi. Making a 2D top down space shooter (think asteroids) using sprites as ships. I'm having huge trouble getting npc ships to look at their target and not disappear. I've had success using 'LookAt' and then correcting the rotation but this method means that whenever an npc changes its target, it snaps directly to looking at the next one, which looks bad. I need them to turn gradually, or at least to dampen the LookAt funciton. It should only rotate on the Z axis, or else it disappears since it's a 2D sprite.

There's a huge amount of threads on this and believe me, I've probably read them all at this stage, so please don't link me to more of them without having a gander at the code I'm using below to see if you can fix something or recommend a different method entirely.

 private GameObject player;
 public float rotatespeed = 2;
 public float damping = 10;
 
 private Quaternion rotation;
 
 float MaxClamp = 0.0f;
 float MinClamp = 0.0f;
 
 private Quaternion Rotation;
 
 void Awake()
 {
     player = GameObject.FindGameObjectWithTag ("Player");
     Rotation = transform.rotation;
 }
 
 void FixedUpdate()
 {
 rotation = Quaternion.LookRotation (player.transform.position - transform.position, Vector3.forward);
 transform.rotation = Quaternion.Lerp (transform.rotation, rotation, Time.deltaTime * rotatespeed/damping);
 
 
 transform.Rotate(new Vector3(0,-90,-90),Space.Self);
 //correcting the original rotation
 //can't get the ship to orient itself correctly
 
 }
 
 void LateUpdate()
 {
 Rotation.eulerAngles = new Vector3(
 Mathf.Clamp (transform.rotation.eulerAngles.x, MinClamp, MaxClamp), 
 Mathf.Clamp (transform.rotation.eulerAngles.y, MinClamp, MaxClamp), 
 transform.rotation.eulerAngles.z); 
 //DIDN'T WORK
 }

I've commented in/out several parts of this to see what I could get. The Late Update part doesn't seem to do anything at all, though I believe it's the most correct way to orient my 2d sprite. The behaviour I get from the first part of Update is that the sprite rotates to point its broad side (the visible side) towards the player, therefore I can't see it anymore from the camera position. I've tried all combinations of telling it the 'World Up' in the function (forward, up, right, and negatives of those) but it always rotates to be invisible from the camera.

The second part of update, if I leave it in, causes the ship to basically flip out; rotating around all axes constantly so it looks like a blurry, pointy mess. This is odd because it's the exact same line of code I was using to correct my LookAt rotation when I was using that before, and it worked then, except for the 'snapping' to a new target that I mentioned at the top.

Sorry for the length, but I just want to get across what's happening. I need very specific help, as I feel I've been through every search result on the topic and tried every aspect of every suggestion on those other pages. I thought it best to get my specific problem detailed here.

Thanks if you've read this far :P

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by robertbu · Oct 09, 2014 at 08:31 PM

I've posted the 2D LookAt a number of times, but here it is again. Assumes:

  • Standard setup of action on the XY plane with the camera looking towards positive 'z'.

  • That the front side of the sprite is the right side.


    Vector3 dir = player.transform.position - transform.position; float angle = Mathf.Atan2(dir.y, dir.x) Mathf.Rad2Deg; Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward); transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime speed);

Note that by using Slerp, you will get an eased rotation. If you don't want eased, change Slerp() to RotateTowards() and increase 'speed' (will be degrees per second).

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 JtheSpaceC · Oct 10, 2014 at 12:53 AM 0
Share

Thanks a mil. 4 lines of code that work perfectly. You've no idea how long I've been held up trying to get this working. I'll have to look up what Atan2 is really doing, and I had to rotate my original sprite 90 degrees to match your code, as I couldn't see how to adjust the code itself to match a sprite with 'forward' at 12 o clock. But thanks so much. A solution that works!

avatar image robertbu · Oct 10, 2014 at 02:07 AM 0
Share

You can use a sprite where 'up' is the front by adding 90.0 to 'angle' before passing it to AngleAxis().

avatar image $$anonymous$$ · Nov 15, 2014 at 04:12 PM 0
Share

@robertbu king of rotations! Thanks (again)!

avatar image RASTRUM · Mar 17, 2017 at 01:42 PM 0
Share

Amazing you did it yet again!!

avatar image
0

Answer by saramul · Jun 06, 2016 at 07:12 AM

Vector3 dir = player.transform.position - transform.position Quaternion rotation = Quaternion.LookRotation(new Vector3(dir.x, 0, dir.z)); transform.rotation = rotation;

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[2D] How do you rotate a 2D sprite left and right with C#? 2 Answers

Finding the centre of two touches 2 Answers

Quaternion Rotate Towards Y Value 1 Answer

Sprite "face" second sprite only on Z axis 2 Answers

Mirroring sprite around arbitrary axis causes collision issues 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