I am using the default camera in my game but it isn’t recognising it. I thought that you just wrote ‘Camera’ but apparently not. I got two errors:
- Assets\InputHandler.cs(26,30): error CS1061: ‘PlayerControls’ does not contain a definition for ‘Camera’ and no accessible extension method ‘Camera’ accepting a first argument of type ‘PlayerControls’ could be found (are you missing a using directive or an assembly reference?)
- Assets\PlayerLocomotion.cs(29,40): error CS1061: ‘Camera’ does not contain a definition for ‘Transform’ and no accessible extension method ‘Transform’ accepting a first argument of type ‘Camera’ could be found (are you missing a using directive or an assembly reference?)
Here is my code:
Player Locomotion Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JG
{
public class PlayerLocomotion : MonoBehaviour
{
Transform cameraObject;
InputHandler inputHandler;
Vector3 moveDirection;
[HideInInspector]
public Transform myTransform;
public new Rigidbody rigidbody;
public GameObject normalCamera;
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
[SerializeField]
float rotationSpeed = 10;
void Start()
{
rigidbody = GetComponent<Rigidbody>();
inputHandler = GetComponent<InputHandler>();
cameraObject = Camera.main.Transform;
myTransform = transform;
}
public void Update()
{
float delta = Time.deltaTime;
inputHandler.TickInput(delta);
moveDirection = cameraObject.forward * inputHandler.vertical;
moveDirection += cameraObject.right * inputHandler.horizontal;
moveDirection.Normalize();
float speed = movementSpeed;
moveDirection *= speed;
Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
rigidbody.velocity = projectedVelocity;
}
#region Movement
Vector3 normalVector;
Vector3 targetPosition;
private void HandleRotation (float delta)
{
Vector3 targetDirection = Vector3.zero;
float moveOverride = inputHandler.moveAmount;
targetDirection = cameraObject.forward * inputHandler.vertical;
targetDirection = cameraObject.right * inputHandler.horizontal;
targetDirection.Normalize();
targetDirection.y = 0;
if(targetDirection == Vector3.zero)
targetDirection = myTransform.forward;
float rs = rotationSpeed;
Quaternion tr = Quaternion.LookRotation(targetDirection);
Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
myTransform.rotation = targetRotation;
}
#endregion
}
}
Input Handler Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JG
{
public class InputHandler : MonoBehaviour
{
public float horizontal;
public float vertical;
public float moveAmount;
public float mouseX;
public float mouseY;
PlayerControls inputActions;
Vector2 movementInput;
Vector2 cameraInput;
public void OnEnable()
{
if (inputActions == null)
{
inputActions = new PlayerControls();
inputActions.PlayerMovement.Movement.performed += inputActions => movementInput = inputActions.ReadValue<Vector2>();
inputActions.Camera.performed += i => cameraInput = i.ReadValue<Vector2>();
}
inputActions.Enable();
}
private void OnDisable()
{
inputActions.Disable();
}
public void TickInput(float delta)
{
MoveInput(delta);
}
private void MoveInput(float delta)
{
horizontal = movementInput.x;
vertical = movementInput.y;
moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
mouseX = cameraInput.x;
mouseY = cameraInput.y;
}
}
}