C#C
C#3y ago
! ZafraFam

New to Coding

Hey guys, so I have written a player movement/camera controller however I ended up messing smoething up and I do know the fix already. Basically what I need help with is learning how to rewrite this portion of my code to make it valid and working. Here is my script;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    [SerializeField] float moveSpeed = 5f;

    CameraController cameraController;
    private void Awake()
    {
        CameraController controller = Camera.main.GetComponent<CameraController>();
    }
    private void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        var moveInput = (new Vector3(h, 0, v)).normalized;

        var moveDir = cameraController.PlanarRotation * moveInput;

        transform.position += moveDir * moveSpeed * Time.deltaTime;
    }
}

The issue is I assigned a local variable CameraController controller in the Awake() method, the field CameraController cameraController; on class level will not be given a value but I'm unsure on how I would rewrite it to get my character moving. Let me make it clear that I don't just want the answer or anything like that, if possible I would like someone to actually explain the steps so I can learn how to solve my mistake and prevent it in the future.
Was this page helpful?