- Home /
Transform Rotation - Maths Problem
Hello,
I'm trying to figure out a rotation problem here.
My scene camera rotates by using my iOS gyro (on an X, Y, Z and W axis)- so basically, if I rotate my phone around my physical room, the game scene will also be 'copying' my rotations on the scene camera.
Now, what I have is a 'hand' model as a child, within the camera, so it rotates with the camera - so it looks like it never rotates to the viewer of the camera.
Hear me out - when I rotate the actual device sideways (Z axis), I want the hand model to always stay the correct way up. Let me do an illustration (It's hard to explain):
^ This is what I WANT to happen. Right now - the hand just stays at the default orientation (But still moves with the camera, so its always in view, in front of the camera).
All thats changing my camera rotation is this:
rotFix = Quaternion(0,0,1,0);
var camRot : Quaternion = gyro.attitude * rotFix;
transform.localRotation = camRot;
Any ideas how I can achieve this with my hand? Thanks!
--- EDIT ---
A good example I found on the app store is a game called AR Zombies - When you hold the device and rotate around your actual room, the game moves with your rotation (This is already working fine). But, if you rotate your device sideways (left and right), notice how the character (weapon) is always dynamically at the bottom (it rotates with the device).
Answer by Wuzseen · Aug 13, 2013 at 10:08 PM
I believe you could achieve the effect you're searching for by putting your hand on one layer and drawing it with a separate camera.
So you would have a new hand layer and a new camera. The camera would be set to just drawing that hand layer. Your main camera would be set to drawing everything but that hand--as well as set to depth only. Depth only being a clear flags option.
This is a common method for using elements in 3D space as GUI elements though it's also a popular option for drawing hands and what not to the main camera so that is separate from that camera's rotation in some respects.
The effect is that the hand is now constantly drawn to the screen in a static position while the rest of the world moves fine.
EDIT: I reread your question and understand it a bit better now. In which case I don't believe what I typed above will be the solution you're looking for--I will leave it up there as it is still tangentially related. You could always define the behavior of the hand in a script so that it mimicks the motion you want it to. It doesn't have to be a child of your camera--which is the only way, save for a script, it would inherit the rotation of your camera.
Thanks for the reply - although I don't think this will achieve the affect I'm looking for - The hand wont rotate/move with the camera. It needs to move with the camera in sync. It's really hard for me to explain.
A good example I found on the app store is a game called AR Zombies - When you hold the device and rotate around your actual room, the game moves with your rotation (This is already working fine). But, if you rotate your device sideways (left and right), notice how the character (weapon) is always dynamically at the bottom (it rotates with the device).
I think I get it--you want it to be more like a head and your hand follows you as if its part of your body.
In that case I see two options. Roll your own script that calculates the deltas of position of the main camera and then move your hand a factor of that delta each frame. Alternatively you can use Vector3.lerp in a semi different than usual way, this code would sit on your hand:
transform.position = Vector3.Lerp(transform.position, Camera.main.transform.position, .8);
I haven't had the chance to test it, but this should keep your hand moving 80% of the distance the camera has. $$anonymous$$ore to the point, it simply follows its position rather than rotate with it.
Answer by chelnok · Aug 14, 2013 at 09:37 AM
If i got it right, you need to use Quaternion.Inverse:
yourHandTransform.rotation = Quaternion.Inverse(gyroRotation);
i dunno how gyro gives you rotation info, but i'm sure your get the point.
I read that Unity automatically retrieves gyro info when you use accelerometer. The way we use the accelerometer is actually what gyro info looks like. Accelerometer in fact shows the actual acceleration of the phone in different orientations and shouldnt give you positional info at all! Weird eh?