After experimenting with this problem today, I discovered that my character’s camera control script was forcing the parent player object to always face positive Z when starting, which is causing issues with designing levels, and where I am able to place things and after failing to work out the problem the good ol’ fashioned way (changing stuff til’ it works), I thought I should ask here.
I can’t remember where I got this script, it may have been from a YouTube video about a year back about character controllers but I can’t really remember; But that doesn’t change or help the issue.
I believe that somewhere around line 37 the main character’s (variable: character) rotation is being forced into this position because until that point, mouseLook.x has no input, but I cannot be sure.
Please ignore the strange input names, these are being fed through cInput, a third-party input asset.
using UnityEngine;
public class CustomMouseLook : MonoBehaviour {
public static CustomMouseLook playerCamera;
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
private float maxSpeed = Mathf.Infinity;
private float smoothTime = 0.3f;
private bool isZoomed;
GameObject character;
void Start ()
{
character = this.transform.parent.gameObject;
}
void Update ()
{
if (!PlayerControl.playerControl.isPaused)
{
Vector2 md = new Vector2(cInput.GetAxis("MHorizontal"), cInput.GetAxis("MVertical"));
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, -90f, 90f);
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
}
if(cInput.GetKeyDown("Zoom"))
{
if(!isZoomed)
{
GetComponent<Camera>().fieldOfView = 20f;
isZoomed = true;
}
else
{
GetComponent<Camera>().fieldOfView = 70f;
isZoomed = false;
}
}
}
}