- Home /
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.
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.
Your answer
Follow this Question
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