Not feeling right about my code.. Is this how I should be doing it?

I’m not just looking for some help with code, I am just looking for some professional advice from programmers. Is it normal to feel like I am not doing something correct at all, and there is a better way to be doing it? How do I overcome this?

I am working on a first person shooter, and I want to make my own first person controller and mouse look scripts. Here my how I have my hierarchy for the controller set up:

Why do I feel like this is incorrect? Here is my script for mouse looking aswell( it works fine ):

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
	private Camera cWorld;
	private Camera cView;
	
	// Mouse look
	private Vector2 mouseInput;
	private Vector2 mouseSensitivity = new Vector2(15f, 15f);
	private float fCamPitch;
	private float fCamYaw;
	
	public void Start () {
		controller = GetComponent<CharacterController>();
		cWorld = transform.FindChild("World Camera").GetComponent<Camera>();
		cView = transform.FindChild("View Camera").GetComponent<Camera>();
	}
	
	public void Update () {
		updateMouseLook();
	}
	
	private void updateMouseLook(){
		mouseInput.x = Input.GetAxis("Mouse X");
		mouseInput.y = Input.GetAxis("Mouse Y");
		
		fCamPitch -= mouseInput.y * mouseSensitivity.y;
		fCamYaw += mouseInput.x * mouseSensitivity.x;
		
		fCamPitch = Mathf.Clamp(fCamPitch, -60, 60);
		
		Quaternion lookRotation = Quaternion.Euler(new Vector3(fCamPitch, fCamYaw, 0));
		cWorld.transform.localRotation = lookRotation;
		cView.transform.localRotation = lookRotation;
		
		Quaternion playerRotation = Quaternion.Euler(new Vector3(0, fCamYaw, 0));
		transform.localRotation = playerRotation;
	}
}

This happens to me every time I start a game; I feel like I am doing it all wrong and I just want to quit, even though the script is doing what I want it to. I really just want to overcome this.

1366890--68439--$srtSMm8.png

Your code is fine. There’s potential for very minor improvements, but nothing really worth mentioning.

Your setup is questionable, however I havnt worked on an FPS at this stage, so I see no real problem there either, and if it works…

why do you have two cameras attached to the player?

considering one is called “worldcamera” …

does it need to follow the exact movements of the player? is it a part of the player?

basically ask yourself “why?” a lot. We all get the “is this the best way of doing things?” feeling, but keep going. If you’re just starting out you’ll do things completely backwards, look at it at the end and think “it’s so obvious that is backwards”… and that just shows that you learnt something getting there :slight_smile:

There are a few reasons he might have two camera’s

I have 4-5 in my current game scene.

  1. Skybox only
  2. Main camera
    3-5. GUI/HUD

I would give you the more general advice of heavily commenting your code when you are doing something new or just learning to code in general. At the start of your script describe what it is the script should be doing in general terms and then describe exactly what you are doing in each line of code and you will quickly see if you are making unneccessary steps.

This might seem like a lot of work, and perhaps a bit silly, but it forces you to really think through each step of your code and understand what it is your script is doing.

The world camera is the players eyes. The view camera is seperate because it renders the players viewmodel on top of the world camera, so that it does not clip through the game geometry.