Update #001:
Alright So I have solved the issue with getting the camera to snap back properly. Here is what I changed/added:
//Looks for when the freelook key gets pressed
if(Input.GetKeyDown(KeyCode.F))
{
transformRotationY = yRotation; //Saves the y position that the player is facing
transformRotationX = xRotation; //Saves the x position that the player is facing
//Debug.Log (transformRotation);
//Check if the player is holding the free look key (will be changed to a button later)
if(Input.GetKey(KeyCode.F))
{
isFreeLook = true;
//Sets the rotation axes, and look constraints
yRotation += Input.GetAxis ("Mouse X") * lookSensitivity;
xRotation += Input.GetAxis ("Mouse Y") * lookSensitivity;
xRotation = Mathf.Clamp(xRotation, -90, 90);
//yRotation = Mathf.Clamp(yRotation, -90, 90); //This doesn't work yet, still working on it.
}
}
else //Player is not holding the free look key
{
//Snaps the camera back to the direction the player is facing
if(isFreeLook)
{
if(Input.GetKeyUp(KeyCode.F))
{
isFreeLook = false;
yRotation = transformRotationY;
xRotation = transformRotationX;
}
}
Now just to sort out the Clamp issues.
Original Post:
Alright so I have searched these forums for this and can’t seem to find it. Some people ask and others mention a free look that isn’t what I am looking for.
So if you’ve ever played ArmA or a game featuring this mechanic, there is a free look where, when holding a key, you’re able to look around while moving and not affecting your direction. I am trying to achieve this and I am sure that the answer is so simple. I’ve prototyped it, and I’ve gotten the free look to work. The problem I am having is a silly one, my fault for setting it up like this.
When you’re standing still and you hold the free look key, you can move the camera around freely. When you release it the camera returns to it’s original position. You’d think that’s fine right? Wrong.
For prototyping purposes I’ve made the camera snap back to, not the direction of the transform it’s attached, but to zero on the y-axis and zero on the x-axis. That is question number one that i am sure the answer is stupidly easy, is how can I get the camera to snap back to the direction the transform is facing. I’ve tried a few different methods, all of which don’t work, or I am not entirely clear on how to use them properly (documentation didn’t really help).
My second problem too is that I’ve limited the freelook on the y-axis to 90 degrees in either direction. As you can imagine this limits it to 90 degrees period. If i turn my capsule, 180 degrees, then hit the free look key, it rotates the camera to -90 or 90. which ever it’s closest to, upon releasing the key, it snaps the camera back to zero, meaning you’d now be looking behind the character. I know obviously why this is happening, but for some reason I can’t seem to find the solution. What i need to happen, is reset the camera to face the direction of the player model, and also limit their look rotation to 90 degrees to the left or right while using free look, based again on which direction the player model is facing.
I don’t need a 100% answer, maybe just a little guidance, a tip or two to point me in the right direction. Here is my prototype code:
using UnityEngine;
using System.Collections;
public class LookScript : MonoBehaviour {
//Variables
public float lookSensitivity = 5f;
private float yRotation;
private float xRotation;
private float curYRot;
private float curXRot;
public float yRotV;
public float xRotV;
public float lookSmoothDamp = 0.1f;
public bool isFreeLook = false;
//Different rotation axes
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseX;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Check if we're able to rotate on the X and Y axes
if(axes == RotationAxes.MouseXAndY)
{
//Check if the player is holding the free look key (will be changed to a button later)
if(Input.GetKey(KeyCode.F))
{
isFreeLook = true;
//Sets the rotation axes, and look constraints
yRotation += Input.GetAxis ("Mouse X") * lookSensitivity;
xRotation += Input.GetAxis ("Mouse Y") * lookSensitivity;
xRotation = Mathf.Clamp(xRotation, -90, 90);
yRotation = Mathf.Clamp(yRotation, -90, 90);
}
else //Player is not holding the free look key
{
//Snaps the camera back to zero (This needs to either be local, or use the character's direction to reset itself)
if(isFreeLook)
{
yRotation = 0;
xRotation = 0;
isFreeLook = false;
}
//We're not holding the free look key, and we're using the X and Y axes. Sets look axes, look sensitivity, and cam constraints
yRotation += Input.GetAxis ("Mouse X") * lookSensitivity;
xRotation += Input.GetAxis ("Mouse Y") * lookSensitivity;
xRotation = Mathf.Clamp(xRotation, -90, 90);
}
//Smooths the camera rotation, eliminates the jittery movement
curXRot = Mathf.SmoothDamp(curXRot, xRotation, ref xRotV, lookSmoothDamp);
curYRot = Mathf.SmoothDamp(curYRot, yRotation, ref yRotV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(curXRot, curYRot, 0);
}
else if(axes == RotationAxes.MouseX) //If we're only using the Mouse X input (Y axis)
{
//Disables rotation of the player model if we're holding free look
if(Input.GetKey(KeyCode.F))
{
transform.Rotate(0, 0, 0);
}
else //not holding freelook
{
//Allow character to rotate on the Y axis only (Used on the character model to allow for player to control their direction with the mouse)
transform.Rotate (0, Input.GetAxis("Mouse X") * lookSensitivity, 0);
}
}
else //If we're only using the Mouse Y input (X axis)
{
//Set input, and look sensitivity, and cam constraints
xRotation += Input.GetAxis ("Mouse Y") * lookSensitivity;
xRotation = Mathf.Clamp(xRotation, -90, 90);
//Smooths camera movement
curXRot = Mathf.SmoothDamp(curXRot, xRotation, ref xRotV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(curXRot, 0, 0);
}
}
}
Any and all help is deeply appreciated.