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
1
Question by ReContraChanfle · Feb 20, 2014 at 05:04 AM · gameobjecttransformquaternionrotate

How rotate only z axis of Gameobject (2D)

Hi, i want to modify the value of the Z axis (rotation) of a Gameobject, without modify the other axis (x,y), i tried with: Transform.Rotate, Quaternion.Angleaxis, Quaternion.Euler, but with no results, i know this a noob question, but i cant figure out, how to do it. Thanks!

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 robertbu · Feb 20, 2014 at 05:21 AM 0
Share

All of the things you tried should have worked, so there is something else going on here. If you post your code, maybe we can figure out what is going on. $$anonymous$$y guess is that you have more than one script (or more than one rotation call) impacting the rotation.

avatar image ReContraChanfle · Feb 20, 2014 at 05:38 AM 0
Share

now, i achieved rotate the gameobject,with Transform.lookAt, to follow the player, but now, the renderer its buggy and dont show the image this its how have to look alt text

and this its buggy sprite alt text

and this its the code for rotate:

 public class Enemy : $$anonymous$$onoBehaviour {
 
     public float angle;
     public Transform iss;
     
     
     
     
     void Update () 
     {
         //angle = $$anonymous$$athf.Atan2 (player.transform.position.x, player.transform.position.y)*$$anonymous$$athf.Rad2Deg;
 
         iss.transform.LookAt (player.transform,Vector3.forward);
 
                 
         }

1 Reply

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

Answer by robertbu · Feb 20, 2014 at 05:50 AM

LookAt() won't work for sprites since LookAt() point the side facing positive 'z' towards the position you specify. This will turn the sprite sideways. The code you have comment out will not work for a couple of reasons. First, you have the parameters backwards. Atan2 should be called as Atan2(Y,X), but you are calling it Atan2(X,Y). Second, when you do the calculation, you need a direction. Unless the object you are rotating is at the origin, you are passing a position, not a direction to the Atan2(). So the angle returned is the angle from Vector2.zero, not from the object you are trying to rotate. I'm guessing some since I have incomplete information, but this is probably in the direction you need to go:

 Vector3 dir = player.transform.position - iss.transform.position;
 float angle = Mathf.Atan2(dir.y, dir.x) * mathf.Rad2Deg;
 iss.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Comment
Add comment · Show 3 · 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 ReContraChanfle · Feb 20, 2014 at 06:04 AM 0
Share

that its what i was looking for, recently i figured out that its dint was a bug, it was the object rotating on (y,x), the parameters are inverted because i am co$$anonymous$$g from SDL, in sdl the order of axis its x,y, i only have a week on unity and c#, anyways, appreciate your answer, thanks!

avatar image shwetakalra · May 08, 2017 at 01:32 PM 0
Share

Thanku roberty it helps for me also....i was searching for this from 2 days i tried quaternioin,lookat,euler angles but got fail everytime.Now i got it..Thanx a lot

avatar image Chintan-Kansagara · Dec 15, 2017 at 07:29 AM 0
Share

hi my 2d sprite character rotate in hit point but character rotate in turn + move and hit direction to move now my character directly hit point to rotate.

code hare Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit = new RaycastHit(); if (Physics.Raycast (ray, out hit, 100)) { Vector3 hitPoint = hit.point;

         float dist = Vector3.Distance(hitPoint, transform.position);
         Debug.Log(dist);

         targetDir = hitPoint - transform.position;

         float angle = $$anonymous$$athf.Atan2(targetDir.y, targetDir.x) * $$anonymous$$athf.Rad2Deg;
         //transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, angle), 20 * Time.deltaTime);    
         //Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
         //transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 1);

     }

plz help me..

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

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

Related Questions

Write a C# script to record GameObject rotation? 4 Answers

How get the rotation of an GameObject? 3 Answers

Instantiate GameObject towards player 0 Answers

Adding Rotation to a Gameobject 1 Answer

Rotation Jumping values (0 to 180) 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