• 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 Steven912 · Aug 19, 2018 at 11:58 PM · rotationeditorrotaterotate objectrotatearound

How to rotate an object according to the camera's view?

I HAVE ATTACHED A DIAGRAM DISPLAYING WHAT I MEAN

Basically, my goal is this: I want a script I can attach to an object that rotates it according to the camera's view. As in, if a cube with the script attached is in front of the camera, and I press W, it rotates upward so that according to the camera, the cube face that was facing the camera is now facing north, the cube face that was facing south is now facing the camera, etc. I've been able to almost do this using quaternion.angleAxis and then lerping, but it rotates the object according to its own orientation, not the cameras, and that leaves the rotation feeling random as it does not act like I described above. I would very much appreciate any help! Thanks. alt text

explanationofrotation.png (21.6 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by coinnip_unity · Aug 20, 2018 at 09:14 AM

Here Simple code for rotate your object considering camera view

if the camera transform is rotated, this makes your cube rotate correct direction for view

     if (Input.GetKeyDown("a"))
     {
         cubeTransform.Rotate(cameraTransform.up, 90, Space.World);
     }
     if (Input.GetKeyDown("d"))
     {
         cubeTransform.Rotate(cameraTransform.up, -90, Space.World);
     }
     if (Input.GetKeyDown("w"))
     {
         cubeTransform.Rotate(cameraTransform.right, 90, Space.World);
     }
     if (Input.GetKeyDown("s"))
     {
         cubeTransform.Rotate(cameraTransform.right, -90, Space.World);
     }

  • "cubeTransform" for your object to rotate,

  • "cameraTransform" for your camera object's transform

code tested on 2017.3.1p4

Comment
Add comment · Show 2 · 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 Steven912 · Aug 23, 2018 at 02:42 AM 0
Share

Thanks so much! You nailed it! The only thing is I'm trying to get it rotate over time as well, just so it's smoother, you know? I'll post my completed code as soon as I do it, but thanks so much.

avatar image Steven912 Steven912 · Aug 23, 2018 at 04:29 AM 0
Share

This is how I got the code above working exactly like it normally does, but rotating over time. I'm sure there are way better ways of doing this, but this is what works for me and its late lol.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class rotating : $$anonymous$$onoBehaviour {
 
     public bool ableToRotate = true;
 
     public Camera camera;
 
     private  bool A$$anonymous$$eyRotate;
     private  bool W$$anonymous$$eyRotate;
     private  bool S$$anonymous$$eyRotate;
     private  bool D$$anonymous$$eyRotate;
 
 
     private int workingInt;
     private int workingIntLimit = 6;
 
     void Start(){
         camera = Camera.main;
 
     }
 
     void Update ()
     {
 
         if (ableToRotate) 
         {
             if (Input.Get$$anonymous$$eyDown("a"))
             {
                 A$$anonymous$$eyRotate=true;
             }
             if (Input.Get$$anonymous$$eyDown("d"))
             {
                 D$$anonymous$$eyRotate = true;
 
             }
             if (Input.Get$$anonymous$$eyDown("w"))
             {
                 W$$anonymous$$eyRotate = true;
 
             }
             if (Input.Get$$anonymous$$eyDown("s"))
             {
                 S$$anonymous$$eyRotate = true;
 
             }
 
         }
 
     
         if (A$$anonymous$$eyRotate) {
             ableToRotate = false;
             gameObject.transform.Rotate(camera.transform.up, 15, Space.World);
             workingInt++;
             if (workingInt >= workingIntLimit) {
                 A$$anonymous$$eyRotate = false;
                 workingInt = 0;
                 ableToRotate = true;
             }
         }
         else if (D$$anonymous$$eyRotate) {
             ableToRotate = false;
             gameObject.transform.Rotate(camera.transform.up, -15, Space.World);
             workingInt++;
             if (workingInt >= workingIntLimit) {
                 D$$anonymous$$eyRotate = false;
                 workingInt = 0;
                 ableToRotate = true;
             }
         }
         else if (W$$anonymous$$eyRotate) {
             ableToRotate = false;
             gameObject.transform.Rotate(camera.transform.right, 15, Space.World);
             workingInt++;
             if (workingInt >= workingIntLimit) {
                 W$$anonymous$$eyRotate = false;
                 workingInt = 0;
                 ableToRotate = true;
             }
         }
         else if (S$$anonymous$$eyRotate) {
             ableToRotate = false;
             gameObject.transform.Rotate(camera.transform.right, -15, Space.World);
             workingInt++;
             if (workingInt >= workingIntLimit) {
                 S$$anonymous$$eyRotate = false;
                 workingInt = 0;
                 ableToRotate = true;
             }
         }
 
 
     }
 
 
 
 }
 
 

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

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

Rotate object in the direction where it is going 1 Answer

rotate object around another object 1:1 1 Answer

Quaternion, eulerAngles, localEulerAngles Driving me mad 1 Answer

I want to use LookAt but based on the object original rotation 1 Answer

transform.Rotate will not work 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