I'm getting this error on public event Action OnBeforeMove()
and I don't know why. What am I missing?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
[SerializeField] float mouseSensitivity = 3f;
[SerializeField] float movementSpeed = 10f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float mass = 1f;
[SerializeField] float acceleration = 20f;
[SerializeField] Transform cameraTransform;
public event Action OnBeforeMove;
internal float movementSpeedMultiplier;
CharacterController controller;
Vector3 velocity;
Vector2 look;
PlayerInput playerInput;
InputAction moveAction;
InputAction lookAction;
InputAction jumpAction;
InputAction sprintAction;
void Awake()
{
controller = GetComponent<CharacterController>();
playerInput = GetComponent<PlayerInput>();
moveAction = playerInput.actions["move"];
lookAction = playerInput.actions["look"];
jumpAction = playerInput.actions["jump"];
sprintAction = playerInput.actions["sprint"];
}
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
UpdateGravity();
UpdateMovement();
UpdateLook();
}
void UpdateGravity()
{
var gravity = Physics.gravity * mass * Time.deltaTime;
velocity.y = controller.isGrounded ? -1f : velocity.y gravity.y;
}
Vector3 GetMovementInput()
{
var moveInput = moveAction.ReadValue<Vector2>();
var input = new Vector3();
input = transform.forward * moveInput.y;
input = transform.right * moveInput.x;
input = Vector3.ClampMagnitude(input, 1f);
input *= movementSpeed * movementSpeedMultiplier;
return input;
}
void UpdateMovement()
{
movementSpeedMultiplier = 1f;
OnBeforeMove?.Invoke();
var input = GetMovementInput();
var factor = acceleration * Time.deltaTime;
velocity.x = Mathf.Lerp(velocity.x, input.x, factor);
velocity.z = Mathf.Lerp(velocity.z, input.z, factor);
var jumpInput = jumpAction.ReadValue<float>();
if (jumpInput > 0 && controller.isGrounded)
{
velocity.y = jumpSpeed;
}
controller.Move(velocity * Time.deltaTime);
}
void UpdateLook()
{
var lookInput = lookAction.ReadValue<Vector2>();
look.x = lookInput.x * mouseSensitivity;
look.y = lookInput.y * mouseSensitivity;
look.y = Mathf.Clamp(look.y, -110f, 110f);
cameraTransform.localRotation = Quaternion.Euler(-look.y, 0, 0);
transform.localRotation = Quaternion.Euler(0, look.x, 0);
}
}
I was trying to create an event that basically gives a "warning" that it's about to do the movement calculation logic. I've imported the Input System so it should have worked, but I'm obviously doing something wrong.
CodePudding user response:
Please add using System;
at the top of your class.
See this reference: https://learn.microsoft.com/en-us/dotnet/api/system.action-1?view=net-7.0
CodePudding user response:
Action
is part of the System
namespace; System.Action OnBeforeMove
.
The error you get is where the compiler is when realising something is not right. Therefore you should examine that line and character.
What is OnbeforeMove
, it is an System.Action
.
Why does the compiler nag about it, there is no reference anywhere to System
.
The compiler just doesn't know what to do but to inform you about what it doesn't know:
The type or namespace 'Action'
... the compiler does not even know if Action is a type or namespace. And both is an assumption by the compiler.
(are you missing a using directive or an assembly reference?)
That means exactly what is going on here. The assembly reference to System is missing. Most likely you did add the System.Runtime.dll
as VS does this for you. But having either System.
before Action
or the use of the using System
at the top of the file is needed.
If you get this error:
Did I make a typo!
Did I add a reference?
Is the assambly added at all.
Check the corresponding error page at learn.microsoft.com.
(click the link of the error code in the error-list of VS)