© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
12 replies
! 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;
    }
}
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Need help (New to coding!)
C#CC# / help
3y ago
✅ Im new to coding any tips??
C#CC# / help
8mo ago
Hello guys totally new to coding need some help
C#CC# / help
15mo ago
❔ Coding Project
C#CC# / help
3y ago