- Home /
Free Look Camera Help
Hello, I'm finding a difficult time having a free look camera work with my script. Essentially I'm making an FPS game similar to ARMA III, and I want a certain key, e.g: Z, to allow the camera to move around while the player movement stays in the same direction. All other methods researched did not work with my script. The camera look code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamMouseLook : MonoBehaviour
{
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
public float negativeYClamp = -65f;
public float positiveYClamp = 80f;
private bool freeLookCheck = false;
GameObject character;
// Start is called before the first frame update
void Start()
{
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update()
{
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
mouseLook.y = Mathf.Clamp(mouseLook.y, negativeYClamp, positiveYClamp);
character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
}
}
The camera is also parented to the Player. (Player is parent). Is this the problem?
I have tried methods such as using a bool to represent whether freeLookCheck is true/false, false being normal rotation and true being freeCam rotation, etc.
Thank you for your time and help.
Answer by sacredgeometry · Aug 22, 2019 at 08:41 PM
Nesting GameObjects might give you this functionality with less leg work.
Video
Note:
You could also nest a transform and just move the camera to it instead of using two cameras.
Example
public class CameraController : MonoBehaviour
{
public Transform FreeLookTransform;
public float FreeLookRotationSpeed = 20.0f;
public Camera FPSCamera;
public Camera FreeLookCamera;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
SwitchCamera();
}
CameraMovement();
}
void SwitchCamera()
{
FPSCamera.enabled = !FPSCamera.enabled;
FreeLookCamera.enabled = !FreeLookCamera.enabled;
}
void CameraMovement()
{
if(FreeLookCamera.enabled)
{
// Do Free Look Rotation on
var horizontal = Input.GetAxis("Mouse X");
FreeLookTransform.Rotate(new Vector3(0, horizontal ,0) * FreeLookRotationSpeed);
}
else
{
// This would be your normal FPS mouse movement
}
}
}
Really sorry, but I'm confused at your explanation, could you explain what 'nesting' is?. I was also not planning on using two cameras but was hoping that I could somehow lock the player rotation while allowing the camera to look around. (I can of course create a clamp on the x axis for the camera while this occurs. The Y-Axis already has a clamp as seen before.) Could I use the Rigidbody freeze rotation for this?
P.S: I forgot to mention I'm new to Unity, but I have been learning for about a month, so I still a fairly basic explanation. Thanks for helping. P.P.S The video was very kind of you. Thanks.
Hi there. Nesting is having one thing inside another thing in this case GameObjects.
The video shows exactly what I mean I can make another showing it working but my script does everything except the normal FPS looking.
Player movement would just be handled by a different script and wouldn't need to change.
i.e. you would still be able to move forwards and backwards and strait from left to right its just what forwards means will be locked into the last position because the Free look camera is no longer changing the players orientation.
You dont need a clamp to do this.
Let me do the whole implementation so you can see it working and then upload another video explaining it more simply.