- Home /
Control camera rotation by induvidual axises through Vector3s (for a fixed camera system)
I'm trying to build a fixed camera system reminiscent of MGS2's. So far I've got camera translation working fine with public bools controlling induvidual axises.
I'd like to get rotation working in a similar way (for example, if the camera was static and "yaw" and "lookZ" were true, you'd get an angle similiar to going along the bridges in MGS2 where the camera turns towards the player's forward location), but I'm not sure about how to manipulate Quarternions with Vector3s to acheive this effect.
I've managed to get rotation roughly working for the wallHug and first person modes, but my camera will retain whatever rotation it had last when I go back to panning, so having a defaultRotation function/Quaternion would make the code a lot more functional.
I am quite new to coding so if there's any way to do this more efficiently or I'm just approaching this in the wrong way altogther, advice to that tune and other constructive criticism would also be greatly appreciated.
public float smoothSpeed;
public bool pan;
//the bools I'd like to use to control rotation. Currently not functional
public bool pitch;
public bool roll;
public bool yaw;
public bool followX;
public bool followY;
public bool followZ;
//these are also non functional. Would ideally work with pitch roll and yaw
public bool lookX;
public bool lookY;
public bool lookZ;
public Transform player;
//defines camera translation
Vector3 desiredPos;
Vector3 panSmoothResult;
private Vector3 panPosX;
private Vector3 panPosY;
private Vector3 panPosZ;
private Vector3 panPosXY;
private Vector3 panPosYZ;
private Vector3 panPosXZ;
private Vector3 panPosXYZ;
public Vector3 lookOffset;
public Vector3 panOffset;
public Vector3 wallHugOffset;
public Vector3 firstPersonPosOffset;
//the vars I'd like to use to control rotation
Vector3 desiredRot;
Vector3 rotSmoothResult;
Vector3 pitchX;
Vector3 pitchY;
Vector3 pitchZ;
Vector3 pitchXY;
Vector3 pitchXZ;
Vector3 pitchYZ;
Vector3 rollX;
Vector3 rollY;
Vector3 rollZ;
Vector3 rollXY;
Vector3 rollXZ;
Vector3 rollYZ;
Vector3 yawX;
Vector3 yawY;
Vector3 yawZ;
Vector3 yawXY;
Vector3 yawXZ;
Vector3 yawYZ;
//first person and wallhug vars
public bool wallHug;
Vector3 firstPersonPos;
Vector3 firstPersonTransition;
public bool firstPerson;
public Transform playerHead;
Vector3 wallHugPos;
Vector3 wallHugRot;
Vector3 wallHugTransition;
void FixedUpdate () {
//set vectors
panPosX.Set(player.position.x, 0f, 0f);
panPosY.Set(0f, player.position.y, 0f);
panPosZ.Set(0f,0f, player.position.z);
panPosXY = panPosX + panPosY;
panPosXZ = panPosX + panPosZ;
panPosYZ = panPosY + panPosZ;
panPosXYZ = player.position;
firstPersonPos = playerHead.position + firstPersonPosOffset;
wallHugPos = player.position + wallHugOffset;
/*would like to control the rotation like this through a CameraLook() function simmilar
to the CameraPan() group of functions. There's probably a better way to do it through a master
if statement*/
pitchX.Set(player.position.x,0f,0f);
pitchY.Set(player.position.y, 0f,0f);
pitchZ.Set(player.position.z,0f,0f);
rollX.Set(0f,player.position.x,0f);
rollX.Set(0f,player.position.y,0f);
rollX.Set(0f,player.position.z,0f);
yawX.Set (0f,0f, player.position.x);
yawX.Set (0f,0f, player.position.y);
yawX.Set (0f,0f, player.position.z);
if(firstPerson){
pan = false;
wallHug = false;
FirstPersonPosition();
}
if(!firstPerson && !wallHug){
pan = true;
transform.rotation.SetLookRotation(lookOffset);
}
if(wallHug){
pan = false;
firstPerson = false;
WallHugPosition();
}
if (pan) {
if(followX && followY && followZ && pan){
PanCamera (panPosXYZ);
}
else if(followX && followY && pan){
PanCamera (panPosXY);
}
else if(followX && followZ && pan){
PanCamera (panPosXZ);
}
else if(followY && followZ && pan){
PanCamera (panPosYZ);
}
else if(followX && pan){
PanCamera (panPosX);
}
else if(followY && pan){
PanCamera (panPosY);
}
else if (followZ && pan){
PanCamera (panPosZ);
}
}
}
/// generic functions for panning
public Vector3 PanOffset(Vector3 panPos)
{
desiredPos = panPos + panOffset;
return desiredPos;
}
public Vector3 PanSmooth (Vector3 desiredPosition)
{
panSmoothResult = Vector3.Lerp (transform.position, desiredPos, smoothSpeed);
return panSmoothResult;
}
// functions that control panning
void PanCamera(Vector3 panAxis)
{
PanOffset (panAxis);
PanSmooth (desiredPos);
transform.position = panSmoothResult;
}
void FirstPersonPosition()
{
//would be nice to have this run once before
firstPersonTransition = Vector3.Lerp (transform.position, firstPersonPos, smoothSpeed);
transform.position = firstPersonPos;
transform.rotation = playerHead.rotation;
transform.Rotate(0f, 0f, 90f);
}
void WallHugPosition(){
wallHugTransition = Vector3.Lerp (transform.position, wallHugPos, smoothSpeed);
transform.position = wallHugTransition;
transform.LookAt (playerHead.position);
}
Your answer
Follow this Question
Related Questions
Rotate Camera to the object 0 Answers
camera movments fixed.. character controller without using character controller -.-' 2 Answers
How to have free rotation camera? 1 Answer
Free Orbit Cam and Mouse Aim Cam aren't working together 0 Answers
Unity - Dancing Ball World camera following and rotating system 1 Answer