Camera only moves diagonally

Whenever I start a new first person project, I use this video for both the player movement, and camera control.

It worked every time before, but now for some reason it doesn’t. First problem I encountered, was that the camera couldn’t move left or right, just up and down. I changed something (can’t remember what), and now it’s only moving diagonally, from bottom left to top right, and back.

Here’s the code I wrote down (as there’s no way of copy/pasting the code from the video).

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;

    GameObject character;

    void Start () {
        character = this.transform.parent.gameObject;
    }
  
    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, -90f, 90f);

        transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
        character.transform.localRotation = Quaternion.AngleAxis (mouseLook.y, character.transform.up);
    }
}

I’ve looked over it 5 times by now, and I still can’t find anything that I messed up while writing the code down. My guess is that there is a very tiny mistake I made that I for some reason can’t spot, or that the recent updates changed the unity API in some way.

I also have everything set up the same way the person in the video does (all scripts connected to the right objects, and the same components added to the right objects).

If you find something moving diagonally, you’re almost certainly feeding in (x, x) or (y, y) in a place that should have an (x, y). In this case, I think your line 28 should be mouseLook.x.

2 Likes

Thank you! :smile:
I feel so dumb. Honestly I expected there to be some error like this, but I still feel stupid for not noticing it myself.

Whenever I have to rewrite a code from a video, it later feels like that “find the panda in the imagine” puzzle if something doesn’t work.

1 Like

Ha! I know that feeling. It’s virtually impossible to debug code before you fully understand it.

2 Likes

Sometimes a fresh pair of eyes is the best solution. It helps if they’re experienced eyes like @StarManta 's, but any will do!

1 Like