Character Always Facing Z+

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;
            }
        }
    }
}

Yes, line 37 is definitely going to override any rotation you have set to start. You should be able to set a “starting rotation” by assigned a value in degrees to mouseLook.x. If you need to assign this automatically in Start() or whenever, you can use Atan2 to math out its starting angle:

Vector3 charForward = character.transform.forward;
float theta = Mathf.Atan2(charForward.z, charForward.x) * Mathf.Rad2Deg;
mouseLook.x = theta;

(The final angle might be off by 90 degrees or need to be inverse or something. I generally guess-and-check a little when using Atan2.)

Not exactly ideal, also not sure where to put this.

I was hoping there was just something I could remove or change to fix this.

Not while keeping its ability to turn the character, no.

You can probably put it in Start(). If you’re doing anything fancy with level loading/creation, then you’ll want to do this immediately after loading the level and placing the character (but before the first Update runs).