- Home /
Mouse Orbit on a Different XY Plane?
This has been frustrating me for the past few hours...
I have this script that controls the camera:
//Where x and y are Mouse controlled floats
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0, 0, -newDistance + 0.3) + target.position;
transform.rotation = rotation;
transform.position = position;
And that works fine for orbiting an object in world space. But I want the orbit to take place on the XY Plane associated with the Transform target. The target rotates as well, as I'm using something of a spherical world approach with it. But the camera always stays aligned to the world. I want the camera to rotate with the player but still retain the Y and X from that Euler, just relative to the player.
I've tried everything, including multiplying the Euler by the target rotation, casting the Euler as a direction and getting a LookRotation, and different combinations of the above... I think I need to step back from this. Thanks for the help, ideas are appreciated!
I don't fully understand your question.
But it's very likely localPosition
will help you.
$$anonymous$$ake your camera a child of one of your spheres (or whatever it is you have there) and then move the camera around only "within" that sphere or "marker object".
Hope it helps!
The camera cannot be a child of the target. The camera has to be able to orbit around the object independent of the object's rotation. I just want the camera's y and x Euler to be relative to the target. Here's a picture of what I need:
So, note that I did not say the camera should be a child of the "target".
Rather it's extremely likely the camera has to be a target of "something" - perhaps the room, or perhaps a "marker" on the floor: the "marker" would follow the position of the target, but, simply always be set to the height of the floor.
I can assure you, you pretty much always do orbiting cameras in this way: rather than "calculating", just have an object (call it a marker object if you will) which the camera then orbits around in a simple way.
In some situations that object may be (for example) "the space ship" or "the room the player is in currently" or something like that .. or it may be a "marker" object as I describe above. The "marker" object in the example I gave would have a trivial three-line script, which keeps that marker object, as I say in the example "in the same position as the player but at floor level".
In the first instance you'd unit-test the marker object by using a bright red gizmo ball, and make sire that works. Only then, add the camera as an orbit from that.
I'm afraid I don't understand your scene description, so can't help more specifically.
You should certainly include some screen-shots of your game, a picture is worth 1000 words.
Answer by SunnyChow · Jan 07, 2016 at 07:06 AM
Sorry that English is not my native language. I am still not sure what kind of camera movement you want. What I give you is a rotation completely relative to the target, Mmmm .... how about this one:
rotation = Quaternion.FromToRotation(Vector3.up , target.up) * rotation ;
Actually, SunnyChow's comment worked exactly for what I needed.
rotation = Quaternion.FromToRotation(Vector3.up , target.up) * rotation;
It changes the Camera's up transform into the Player's up, which means the player will always face the correct way relative to the camera. Thanks!
Great!
note for anyone else reading, it is common to do this exactly as I mention above, by making (some sort of) "marker" object that, for example, follows the player (or whatever) around on the floor. You then just orbit that.
(In particular, in this way you can quite easily add camera effects like lag, springiness etc.) Cheers!
Hi, thanks for fixing the formatting! But for the sake of clarity, and the purpose of improving as a developer, I still don't understand how having the camera orbit a "marker" would solve my rotation issues. If the marker simply followed the position of the player, the player could still move upside down without the camera changing... $$anonymous$$aybe I'm missing something here, but could you clarify how it intends to fix the issue? (And sorry for being a nuisance!)
THAN$$anonymous$$ YOU SIR. I've been struggling with an orbit script for a character that can walk on walls for hours now, that simple line made it relative to it's current rotation. Thanks!
Answer by SunnyChow · Jan 07, 2016 at 04:02 AM
var rotation = Quaternion.Euler(y, x, 0);
rotation = target.rotation * rotation ;
var position = rotation * Vector3(0, 0, -newDistance + 0.3) + target.position;
transform.rotation = rotation;
transform.position = position;
"target.rotation * rotation" or "rotation * target.rotation", I am not sure.
No, Quaternion multiplication adds Left-hand rotations to Right-hand. The camera has to keep the orbit position, where the code above just makes the camera spin whenever the target rotates. Both orders of the equation failed. The only time Transform.rotation multiplication operation makes something local to that rotation is when is when it's used with a Vector3 Direction.
All of these calculations are basically wrong :) But you honestly really wouldn't do that, you do it "with game objects", using the calculating power of Transform as your friend
I guess you are talking about parenting the camera to target.
There is some drawback to do this. If i disable any parent GameObject of target, the camera is also disable. If the target is destroyed, the camera is destroyed too unless i write some script to unparent before the destruction. Also, I want the camera move with lerp, it's more tricky to do that when parented.
Beside, to have better performance, you should do calculation with $$anonymous$$ath ins$$anonymous$$d of GameObject