Hello!
I wanted to implement the new InputSystem into my project but i can not fix one error…
I followed this tutorial from Unity:
After following every step like in the video i still get this error, i would appreciate your support
This is my code:
// More About this code can be found using catlikecodeing.com/unity/tutorials/movement
// At the moment only tutorials number 1,2 and 3 has been used.
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;
public class MovingSphere : MonoBehaviour {
[SerializeField, Range(0f, 100f)] public float maxSpeed;
[SerializeField, Range(0f, 100f)] float maxAcceleration = 10f, maxAirAcceleration = 1f;
[SerializeField, Range(0f, 10f)] public float jumpHeight;
[SerializeField, Range(0, 5)] public int maxAirJumps;
[SerializeField, Range(0, 90)] float maxGroundAngle = 25f, maxStairsAngle = 50f;
[SerializeField, Range(0f, 100f)] float maxSnapSpeed = 100f;
[SerializeField, Min(0f)] float probeDistance = 1f;
[SerializeField] LayerMask probeMask = -1, stairsMask = -1;
Rigidbody body;
Vector3 velocity, desiredVelocity;
bool desiredJump;
Vector3 contactNormal, steepNormal;
int groundContactCount, steepContactCount;
bool OnGround => groundContactCount > 0;
bool OnSteep => steepContactCount > 0;
int jumpPhase;
float minGroundDotProduct, minStairsDotProduct;
int stepsSinceLastGrounded, stepsSinceLastJump;
[SerializeField] Transform playerInputSpace = default;
//new Input System
PlayerActions InputActions;
Vector2 movementInput;
void OnValidate () {
minGroundDotProduct = Mathf.Cos(maxGroundAngle * Mathf.Deg2Rad);
minStairsDotProduct = Mathf.Cos(maxStairsAngle * Mathf.Deg2Rad);
InputActions.PlayerControls.Movement.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
}
private void OnEnable()
{
InputActions.Enable();
}
private void OnDisable()
{
InputActions.Disable();
}
void Awake () {
body = GetComponent<Rigidbody>();
OnValidate();
}
void Update () {
Vector2 playerInput;
playerInput.x = movementInput.x;
playerInput.y = movementInput.y;
playerInput = Vector2.ClampMagnitude(playerInput, 1f);
if (playerInputSpace)
{
Vector3 forward = playerInputSpace.forward;
forward.y = 0f;
forward.Normalize();
Vector3 right = playerInputSpace.right;
right.y = 0f;
right.Normalize();
desiredVelocity = (forward * playerInput.y + right * playerInput.x) * maxSpeed;
}
else
{
desiredVelocity =
new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
}
desiredJump |= Input.GetButtonDown("Jump");
}
void FixedUpdate () {
UpdateState();
AdjustVelocity();
if (desiredJump) {
desiredJump = false;
Jump();
}
body.velocity = velocity;
ClearState();
}
void ClearState () {
groundContactCount = steepContactCount = 0;
contactNormal = steepNormal = Vector3.zero;
}
void UpdateState () {
stepsSinceLastGrounded += 1;
stepsSinceLastJump += 1;
velocity = body.velocity;
if (OnGround || SnapToGround() || CheckSteepContacts()) {
stepsSinceLastGrounded = 0;
if (stepsSinceLastJump > 1) {
jumpPhase = 0;
}
if (groundContactCount > 1) {
contactNormal.Normalize();
}
}
else {
contactNormal = Vector3.up;
}
}
bool SnapToGround () {
if (stepsSinceLastGrounded > 1 || stepsSinceLastJump <= 2) {
return false;
}
float speed = velocity.magnitude;
if (speed > maxSnapSpeed) {
return false;
}
if (!Physics.Raycast(
body.position, Vector3.down, out RaycastHit hit,
probeDistance, probeMask
)) {
return false;
}
if (hit.normal.y < GetMinDot(hit.collider.gameObject.layer)) {
return false;
}
groundContactCount = 1;
contactNormal = hit.normal;
float dot = Vector3.Dot(velocity, hit.normal);
if (dot > 0f) {
velocity = (velocity - hit.normal * dot).normalized * speed;
}
return true;
}
bool CheckSteepContacts () {
if (steepContactCount > 1) {
steepNormal.Normalize();
if (steepNormal.y >= minGroundDotProduct) {
steepContactCount = 0;
groundContactCount = 1;
contactNormal = steepNormal;
return true;
}
}
return false;
}
void AdjustVelocity () {
Vector3 xAxis = ProjectOnContactPlane(Vector3.right).normalized;
Vector3 zAxis = ProjectOnContactPlane(Vector3.forward).normalized;
float currentX = Vector3.Dot(velocity, xAxis);
float currentZ = Vector3.Dot(velocity, zAxis);
float acceleration = OnGround ? maxAcceleration : maxAirAcceleration;
float maxSpeedChange = acceleration * Time.deltaTime;
float newX =
Mathf.MoveTowards(currentX, desiredVelocity.x, maxSpeedChange);
float newZ =
Mathf.MoveTowards(currentZ, desiredVelocity.z, maxSpeedChange);
velocity += xAxis * (newX - currentX) + zAxis * (newZ - currentZ);
}
void Jump () {
if (OnGround || jumpPhase < maxAirJumps)
{
stepsSinceLastJump = 0;
jumpPhase += 1;
float jumpSpeed = Mathf.Sqrt(-2f * Physics.gravity.y * jumpHeight);
float alignedSpeed = Vector3.Dot(velocity, contactNormal);
if (alignedSpeed > 0f)
{
jumpSpeed = Mathf.Max(jumpSpeed - alignedSpeed, 0f);
}
velocity += contactNormal * jumpSpeed;
}
}
void OnCollisionEnter (Collision collision) {
EvaluateCollision(collision);
}
void OnCollisionStay (Collision collision) {
EvaluateCollision(collision);
}
void EvaluateCollision (Collision collision) {
float minDot = GetMinDot(collision.gameObject.layer);
for (int i = 0; i < collision.contactCount; i++) {
Vector3 normal = collision.GetContact(i).normal;
if (normal.y >= minDot) {
groundContactCount += 1;
contactNormal += normal;
}
else if (normal.y > -0.01f) {
steepContactCount += 1;
steepNormal += normal;
}
}
}
Vector3 ProjectOnContactPlane (Vector3 vector) {
return vector - contactNormal * Vector3.Dot(vector, contactNormal);
}
float GetMinDot (int layer) {
return (stairsMask & (1 << layer)) == 0 ?
minGroundDotProduct : minStairsDotProduct;
}
}
If you need more informations please tell me