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 oneir0naut0 · May 04, 2014 at 04:43 AM · camerarotationsnaprelative

Rotate object in 90 degrees relative to camera.

I have a more complex game I've been working on, but I'm in a kinda restart phase on it, and trying to get some more basic things working. I'm trying to get rotation of game objects, relative to the camera working.

In "sandboxing" this concept, I have an object (right now just a cube) that I want to eventually rotate based off of input, but I need the rotation to adjust based off of which side of the object the camera is on. Basically, if I were to push up, I want the cube to rotate 90 degrees up and away from the screen. If I rotate the camera to the left side of the object, and push up, I still want the cube to rotate up and away, which would have been a clockwise rotation with the camera in the previous position.

I can get the cube to rotate 90 degrees, but it's relative to the cube itself, so once it rotates, all of the directions change.

Here's some code:

     #pragma strict
      
     public var seconds: float = .2;
      
     private var rotating = false;
      
     function rotateObject (thisTransform : Transform, degrees : Vector3) {
      
         if (rotating) return;
      
         rotating = true;
      
         var startRotation : Quaternion = thisTransform.rotation;
         var endRotation : Quaternion = thisTransform.rotation * Quaternion.Euler(degrees);
         var t : float = 0.0;
         var rate : float = 1.0/seconds;
      
         while (t < 1.0)
         {
             t += Time.deltaTime * rate;
             thisTransform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
             yield;
         }
      
         rotating = false;
      
     }
     
     var rotationAxis : Vector3;
     var dir : String;
      
     function rotate(rotateAngle : Vector3)
     {
         rotateObject(transform, rotateAngle);
     }
     
     function rotateRandom()
     {
         var ranRot = Random.Range(1, 5);
         switch(ranRot)
         {
             case 1 : rotationAxis = Camera.main.transform.up * 90; dir = "Up"; break; 
             case 2 : rotationAxis = Camera.main.transform.up * -90; dir = "Down"; break;
             case 3 : rotationAxis = Camera.main.transform.forward * 90; dir = "Left"; break;
             case 4 : rotationAxis = Camera.main.transform.forward * -90; dir = "Right"; break;
             case 5 : rotationAxis = Vector3.right * 90; dir = "Clock"; break;
             case 6 : rotationAxis = Vector3.right * - 90; dir = "Counter"; break;
         }
         rotate(rotationAxis);
         Debug.Log("Rotating " + dir);
     }
         
     private var period : float = 2.0;
     private var curTime : float;// = Time.time;
     
     function Start()
     {
         curTime = Time.time;
     }
 function Update()
 {
     if ((Time.time - curTime) > period)
     {
         rotateRandom();
         curTime = Time.time;
     }
 }

End Code

Sorry, still kinda in 'sketch' mode, so it does the rotations randomly, I'll add the input stuff later. You can see in the switch statement where I've started trying for the camera relative rotation (switch 1-4) as opposed to the object relative rotation (switch 5-6). Right now, both approaches do the same thing, which basically makes the object rotate randomly as, each time it rotates, the relative 'Up', 'Down', etc. changes.

The camera can rotate freely around the object, both in the circular plane around it, and above and below. Regardless of where the camera is, I need the object to only rotate 90 degrees on six axis: up, down, left, right, clockwise and counterclockwise. I think I might be able to work it out if the camera were always lined up and directly facing the object, but the goal would be for it to be able to be in any position on a 'sphere' around the object.

I hope I'm making sense here. Any help would be greatly appreciated.

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
0

Answer by oneir0naut0 · May 04, 2014 at 06:31 AM

Okay, so I worked with this some more and came up with the following:

 var rotationAxis : Vector3;
 var dir : String;
 var cam : Transform;
 var camRotAxisUp : Vector3;
 var camRotAxisLeft : Vector3;
 var camRotAxisClock : Vector3;
  
 function rotate(rotateAngle : Vector3)
 {
     rotateObject(transform, rotateAngle);
 }
 
 function rotateRandom()
 {
     var ranRot = Random.Range(1, 7);
     camRotAxisUp = transform.InverseTransformDirection(cam.TransformDirection(Vector3.right)).normalized;
     camRotAxisLeft = transform.InverseTransformDirection(cam.TransformDirection(Vector3.up)).normalized;
     camRotAxisClock = transform.InverseTransformDirection(cam.TransformDirection(Vector3.back)).normalized;
     switch(ranRot)
     {
         case 1 : rotationAxis = camRotAxisUp * 90; dir = "Up"; break; 
         case 2 : rotationAxis = camRotAxisUp * -90; dir = "Down"; break;
         case 3 : rotationAxis = camRotAxisLeft * 90; dir = "Left"; break;
         case 4 : rotationAxis = camRotAxisLeft * -90; dir = "Right"; break;
         case 5 : rotationAxis = camRotAxisClock * 90; dir = "Clock"; break;
         case 6 : rotationAxis = camRotAxisClock * -90; dir = "Counter"; break;
     }
     rotate(rotationAxis);
     Debug.Log("Rotating " + dir);
 }

This actually works as long as the camera is facing the object and rotated to 90 degree increments - ie: (0,90,270). However, if the camera is angled otherwise, as it usually will be when circling the object, the object rotation is thrown off to degrees not snapped to 90. I think I'm getting close, but need some help now in figuring out how to convert the random rotations - ie (27.8, 69.6, 293) to the closest 90 degree snapped rotation that is still pointed toward the object.

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

20 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

Related Questions

Relative Movement Problem 1 Answer

Rotating a plane so that its top is parallel to the camera viewport 1 Answer

camera slides and bounces while moving C# 1 Answer

Global movement 0 Answers

Rotate object ON THE CAMERA UP 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