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 Frede1012 · Jul 01, 2013 at 09:18 PM · c#2drotationspriteaxis

Rotating a specific axis towards a object

Hello everyone!

After a few hours of research, i was not able to find a solution to my problem. I would like to make my 2D sprite "look at" another 2D sprite and then go towards it. But i am stuck with the rotating, i tried using LookAt() but then the sprite rotates on a wrong axis and the sprite flies out of the picture.

So basicly i just want it to rotate towards the other sprite but only on the Z-axis.

Comment
Add comment · Show 2
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 Owen-Reynolds · Jul 01, 2013 at 09:30 PM 0
Share

Did you search "unity rotate one axis"? There are a lot of questions here like yours.

The trick is usually to "flatten" the look point by copying and setting the x,y or z to your own. If you want to use x or y to look (LookAt aims Z,) the child trick can fix it. Very common question/answers about that stuff.

avatar image Frede1012 · Jul 01, 2013 at 09:41 PM 0
Share

Yes, i searched for that, but i didn't find any solutions. I don't understand what you mean, i want it to "LookAt" another sprite so i can't just set the rotation?

3 Replies

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

Answer by Frede1012 · Jul 02, 2013 at 02:56 PM

I fixed this myself by switching to Orthello 2D Framework and using a built in feature for sprites called RotateTowards()!

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
1

Answer by robertbu · Jul 01, 2013 at 09:42 PM

@Owne Reynolds mentions the typical solution, but I've found it does not work when you want to rotate around the 'Z' axis. It does look in the right place, but it can result in the object flipping. As an alternate solution, you can use Mathf.Atan2() and do your rotations in 2D. The following code assumes the right side of your object is what you want to look at the target. If you are using a plane, consider creating a vertical plane using the CreatePlane editor script.

pragma strict

 var target : Transform;
 var speed = 0.75;
  
 function Update() {
     var v3Dir = target.position - transform.position;
     var angle = Mathf.Atan2(v3Dir.y, v3Dir.x) * Mathf.Rad2Deg;
     transform.eulerAngles = Vector3(0,0,angle);
     
     if (Input.GetKey(KeyCode.W))
         transform.Translate(Vector3.right * speed * Time.deltaTime);
       
 }

I tossed in a Translate towards the target when the 'W' key is pressed.

Comment
Add comment · Show 11 · 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 Frede1012 · Jul 01, 2013 at 09:45 PM 0
Share

I would really like the code in C#!

avatar image robertbu · Jul 01, 2013 at 09:51 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 
 public class $$anonymous$$yLook : $$anonymous$$onoBehaviour {
 
 public Transform target;
 public float speed = 0.75f;
  
 void Update() {
     Vector3 v3Dir = target.position - transform.position;
     var angle = $$anonymous$$athf.Atan2(v3Dir.y, v3Dir.x) * $$anonymous$$athf.Rad2Deg;
     transform.eulerAngles = new Vector3(0,0,angle);
     
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
         transform.Translate(Vector3.right * speed * Time.deltaTime);
       
     }
 }
avatar image Frede1012 · Jul 01, 2013 at 09:57 PM 0
Share

This does not rotate the sprite so it faces the other sprite?

avatar image robertbu · Jul 01, 2013 at 10:19 PM 0
Share

You need to drag and drop the other sprit onto the 'target' variable in the inspector. This code rotates the sprite it is attached to so that the right side of the sprite faces the other sprite.

avatar image Owen-Reynolds · Jul 01, 2013 at 11:31 PM 1
Share

Another way to avoid flipping is to set UP in the lookAt. For a z-spin, the object would likely be facing North or South. So transform.LookAt(somePos, -Vector3.foward); will keep the y axis always pointed South, for example.

Show more comments
avatar image
0

Answer by servival · Jun 27, 2015 at 04:01 PM

      using UnityEngine;
     using System.Collections;
     public class AISmoothLookAT : MonoBehaviour {
     public Transform target;
     public Transform looker;
     public Transform Xsmoothlooker;
     public Transform Ysmoothlooker;
     public float Xspeed = 2f;
     public float Yspeed = 2f;
     // Use this for initialization
     void Start () {
     }
     // Update is called once per frame
     void Update () {
     looker.LookAt (target);
     float YRotation = looker.eulerAngles.y;
     float XRotation = looker.eulerAngles.x;
     Xsmoothlooker.rotation = Quaternion.Slerp(Xsmoothlooker.rotation , Quaternion.Euler(0 , YRotation, 0), Time.deltaTime * Xspeed);
     Ysmoothlooker.rotation = Quaternion.Slerp(Ysmoothlooker.rotation , Quaternion.Euler(XRotation , YRotation, 0), Time.deltaTime * Yspeed);
     }
     }
 
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

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

Related Questions

2D Sprite Animation - Rotate animation on axis 1 Answer

Rotate the weapon sprite with a joystick 0 Answers

Flip over an object (smooth transition) 3 Answers

Unity 2D all objects mash together! 0 Answers

Is there a way to lock my camera's rotation and movement on certain axis? 2 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