Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 PitVital · Jun 14, 2018 at 10:07 PM · rotationclicked

Rotating an GameObject in a 2D plane based on clicking on a game object.

Hi,

I have been trying to solve a problem for a while and I feel like I am going in circles, so I thought I would try here.

Firstly, I apologise for the crudeness of the image.

I have a Circle (Blue) with a Game Object in the center of it (Black Circle). The Camera (Green) is a child of the Black Circle.

I am trying to rotate the camera, to the randomly instantiated Game Objects (Red) When they are selected, by rotating the Black Circle, however, I am running into difficulties when the Black Circles rotation value goes into negative values.

I haven't had great success with this so any help in pointing me in the right direction would be much appreciated.

alt text

bad-image.png (15.8 kB)
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 incorrect · Jun 14, 2018 at 10:12 PM 0
Share

Because you mention angles I assume you use Euler angles. You should know that angles are just a representation of actual rotation defined by Quaternions. So most operations like rotating the objects and making them look at some directions are best done with methods provided by quaternions.

So how do you rotate them? Can you post your code please?

avatar image PitVital incorrect · Jun 15, 2018 at 07:42 AM 0
Share

Hi,

Thanks for your response. Currently I am using Quaternion.FromToRotation, although this code is considering 3D space. With this snippit, my camera rotation group always ends up at 0,0,0.

 Camera myCamera = Camera.main;
 Transform cameraRotator = myCamera.transform.parent.transform;
 
 Vector3 currentPos = cameraRotator.position;
 Vector3 targetDir = transform.position;
 
 cameraRotator.rotation = Quaternion.FromToRotation(currentPos, targetDir);

1 Reply

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

Answer by PitVital · Jun 15, 2018 at 01:31 PM

Hi, so I managed to get something working very close to what I wanted, I think I can tune it in from here.

But I wanted to share my solution so here it is:

 if (Input.GetMouseButtonDown(0))
         {
             // Getting Camera.
             Camera myCamera = Camera.main;
             // Get parent Transform from camera
             Transform cameraRotator = myCamera.transform.parent.transform;
 
             // Get positions of the building in question and the cameraRotator.
             Vector3 currentPos = cameraRotator.position;
             Vector3 targetDir = transform.position;
 
             // Subtracting the Vector3's from each other.
             Vector3 relativePos = targetDir - currentPos;
 
             // Create the Quaternion we need to rotate by. 
             Quaternion rotationDelta = Quaternion.FromToRotation(cameraRotator.transform.up, relativePos);
             
             // Nuke the Y and Z Axis back to zero on the delta
             var angles = rotationDelta.eulerAngles;
             angles.y = angles.z = 0f;
             rotationDelta = Quaternion.Euler(angles);
             
             // Multiply the values together.
             Quaternion targetRotation = cameraRotator.rotation * rotationDelta;
 
             // starting the routine that will allow us to lerp this aforementioned value.
             StartCoroutine(Transition(cameraRotator, targetRotation));
 
         }
 
 IEnumerator Transition(Transform cameraRotator, Quaternion targetRotation)
     {
         float step = 0.0f;
         while (step < 1.0f)
         {
             // The step size is equal to the speed times frame time.
             step += Time.deltaTime * (Time.timeScale / transitionDuration);
             cameraRotator.rotation = Quaternion.Lerp(cameraRotator.rotation, targetRotation, step);
             yield return 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.

Follow this Question

Answers Answers and Comments

118 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 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 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 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 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 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

Align camera and turret aim 0 Answers

Rotate an Object by a spezific value 2 Answers

Set Velocity of Object According to Rotation 2 Answers

How to rotate my object correctly? 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