C
C#2y 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;
}
}
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.
7 Replies
Thinker
Thinker2y ago
Firstly, you're way more likely to get better help in #game-dev or $unity
Thinker
Thinker2y ago
Secondly, you're setting Camera.main.GetComponent<CameraController>() to a variable called controller inside Awake(). If you want to set the field, you should set cameraController instead.
! ZafraFam
! ZafraFamOP2y ago
Ohhh, I see, so would this be correct?
CameraController cameraController = Camera.main.GetComponent<CameraController>();
CameraController cameraController = Camera.main.GetComponent<CameraController>();
Also, I will most certainly join that discord
Thinker
Thinker2y ago
Well, you're still declaring a variable here, but now it has the same name as your field. If you want to assign the field, you just use cameraController = ...; without the type
! ZafraFam
! ZafraFamOP2y ago
Ohhh so
cameraController = Camera.main.GetComponent<CameraController>();
cameraController = Camera.main.GetComponent<CameraController>();
It works now, thanks so much you're a big help I greatly appreciate the responses and the patience, thank you so much
Thinker
Thinker2y ago
yep
Want results from more Discord servers?
Add your server