Search Unity

Resolved New InputSystem GetAxis implementation

Discussion in 'Input System' started by resteqs, May 1, 2021.

  1. resteqs

    resteqs

    Joined:
    Nov 17, 2019
    Posts:
    7
    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
    upload_2021-5-1_13-58-1.png
    This is my code:
    Code (CSharp):
    1. // More About this code can be found using catlikecodeing.com/unity/tutorials/movement
    2. // At the moment only tutorials number 1,2 and 3 has been used.
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using UnityEngine.InputSystem.Utilities;
    6.  
    7. public class MovingSphere : MonoBehaviour {
    8.  
    9.     [SerializeField, Range(0f, 100f)] public float maxSpeed;
    10.     [SerializeField, Range(0f, 100f)] float maxAcceleration = 10f, maxAirAcceleration = 1f;
    11.     [SerializeField, Range(0f, 10f)] public float jumpHeight;
    12.     [SerializeField, Range(0, 5)] public int maxAirJumps;
    13.     [SerializeField, Range(0, 90)] float maxGroundAngle = 25f, maxStairsAngle = 50f;
    14.     [SerializeField, Range(0f, 100f)] float maxSnapSpeed = 100f;
    15.     [SerializeField, Min(0f)] float probeDistance = 1f;
    16.     [SerializeField] LayerMask probeMask = -1, stairsMask = -1;
    17.     Rigidbody body;
    18.     Vector3 velocity, desiredVelocity;
    19.     bool desiredJump;
    20.     Vector3 contactNormal, steepNormal;
    21.     int groundContactCount, steepContactCount;
    22.     bool OnGround => groundContactCount > 0;
    23.     bool OnSteep => steepContactCount > 0;
    24.     int jumpPhase;
    25.     float minGroundDotProduct, minStairsDotProduct;
    26.     int stepsSinceLastGrounded, stepsSinceLastJump;
    27.     [SerializeField] Transform playerInputSpace = default;
    28.     //new Input System
    29.     PlayerActions InputActions;
    30.     Vector2 movementInput;
    31.  
    32.     void OnValidate () {
    33.         minGroundDotProduct = Mathf.Cos(maxGroundAngle * Mathf.Deg2Rad);
    34.         minStairsDotProduct = Mathf.Cos(maxStairsAngle * Mathf.Deg2Rad);
    35.         InputActions.PlayerControls.Movement.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
    36.     }
    37.     private void OnEnable()
    38.     {
    39.         InputActions.Enable();
    40.     }
    41.     private void OnDisable()
    42.     {
    43.         InputActions.Disable();
    44.     }
    45.     void Awake () {
    46.         body = GetComponent<Rigidbody>();
    47.         OnValidate();
    48.        
    49.     }
    50.    
    51.  
    52.     void Update () {
    53.         Vector2 playerInput;
    54.         playerInput.x = movementInput.x;
    55.         playerInput.y = movementInput.y;
    56.         playerInput = Vector2.ClampMagnitude(playerInput, 1f);
    57.         if (playerInputSpace)
    58.         {
    59.             Vector3 forward = playerInputSpace.forward;
    60.             forward.y = 0f;
    61.             forward.Normalize();
    62.             Vector3 right = playerInputSpace.right;
    63.             right.y = 0f;
    64.             right.Normalize();
    65.             desiredVelocity = (forward * playerInput.y + right * playerInput.x) * maxSpeed;
    66.         }
    67.         else
    68.         {
    69.             desiredVelocity =
    70.             new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
    71.         }
    72.  
    73.         desiredJump |= Input.GetButtonDown("Jump");
    74.     }
    75.  
    76.     void FixedUpdate () {
    77.         UpdateState();
    78.         AdjustVelocity();
    79.  
    80.         if (desiredJump) {
    81.             desiredJump = false;
    82.             Jump();
    83.         }
    84.  
    85.         body.velocity = velocity;
    86.         ClearState();
    87.     }
    88.  
    89.     void ClearState () {
    90.         groundContactCount = steepContactCount = 0;
    91.         contactNormal = steepNormal = Vector3.zero;
    92.     }
    93.  
    94.     void UpdateState () {
    95.         stepsSinceLastGrounded += 1;
    96.         stepsSinceLastJump += 1;
    97.         velocity = body.velocity;
    98.         if (OnGround || SnapToGround() || CheckSteepContacts()) {
    99.             stepsSinceLastGrounded = 0;
    100.             if (stepsSinceLastJump > 1) {
    101.                 jumpPhase = 0;
    102.             }
    103.             if (groundContactCount > 1) {
    104.                 contactNormal.Normalize();
    105.             }
    106.         }
    107.         else {
    108.             contactNormal = Vector3.up;
    109.         }
    110.     }
    111.  
    112.     bool SnapToGround () {
    113.         if (stepsSinceLastGrounded > 1 || stepsSinceLastJump <= 2) {
    114.             return false;
    115.         }
    116.         float speed = velocity.magnitude;
    117.         if (speed > maxSnapSpeed) {
    118.             return false;
    119.         }
    120.         if (!Physics.Raycast(
    121.             body.position, Vector3.down, out RaycastHit hit,
    122.             probeDistance, probeMask
    123.         )) {
    124.             return false;
    125.         }
    126.         if (hit.normal.y < GetMinDot(hit.collider.gameObject.layer)) {
    127.             return false;
    128.         }
    129.  
    130.         groundContactCount = 1;
    131.         contactNormal = hit.normal;
    132.         float dot = Vector3.Dot(velocity, hit.normal);
    133.         if (dot > 0f) {
    134.             velocity = (velocity - hit.normal * dot).normalized * speed;
    135.         }
    136.         return true;
    137.     }
    138.  
    139.     bool CheckSteepContacts () {
    140.         if (steepContactCount > 1) {
    141.             steepNormal.Normalize();
    142.             if (steepNormal.y >= minGroundDotProduct) {
    143.                 steepContactCount = 0;
    144.                 groundContactCount = 1;
    145.                 contactNormal = steepNormal;
    146.                 return true;
    147.             }
    148.         }
    149.         return false;
    150.     }
    151.  
    152.     void AdjustVelocity () {
    153.         Vector3 xAxis = ProjectOnContactPlane(Vector3.right).normalized;
    154.         Vector3 zAxis = ProjectOnContactPlane(Vector3.forward).normalized;
    155.  
    156.         float currentX = Vector3.Dot(velocity, xAxis);
    157.         float currentZ = Vector3.Dot(velocity, zAxis);
    158.  
    159.         float acceleration = OnGround ? maxAcceleration : maxAirAcceleration;
    160.         float maxSpeedChange = acceleration * Time.deltaTime;
    161.  
    162.         float newX =
    163.             Mathf.MoveTowards(currentX, desiredVelocity.x, maxSpeedChange);
    164.         float newZ =
    165.             Mathf.MoveTowards(currentZ, desiredVelocity.z, maxSpeedChange);
    166.  
    167.         velocity += xAxis * (newX - currentX) + zAxis * (newZ - currentZ);
    168.     }
    169.  
    170.     void Jump () {
    171.         if (OnGround || jumpPhase < maxAirJumps)
    172.         {
    173.             stepsSinceLastJump = 0;
    174.             jumpPhase += 1;
    175.             float jumpSpeed = Mathf.Sqrt(-2f * Physics.gravity.y * jumpHeight);
    176.             float alignedSpeed = Vector3.Dot(velocity, contactNormal);
    177.             if (alignedSpeed > 0f)
    178.             {
    179.                 jumpSpeed = Mathf.Max(jumpSpeed - alignedSpeed, 0f);
    180.             }
    181.             velocity += contactNormal * jumpSpeed;
    182.         }
    183.     }
    184.  
    185.     void OnCollisionEnter (Collision collision) {
    186.         EvaluateCollision(collision);
    187.     }
    188.  
    189.     void OnCollisionStay (Collision collision) {
    190.         EvaluateCollision(collision);
    191.     }
    192.  
    193.     void EvaluateCollision (Collision collision) {
    194.         float minDot = GetMinDot(collision.gameObject.layer);
    195.         for (int i = 0; i < collision.contactCount; i++) {
    196.             Vector3 normal = collision.GetContact(i).normal;
    197.             if (normal.y >= minDot) {
    198.                 groundContactCount += 1;
    199.                 contactNormal += normal;
    200.             }
    201.             else if (normal.y > -0.01f) {
    202.                 steepContactCount += 1;
    203.                 steepNormal += normal;
    204.             }
    205.         }
    206.     }
    207.  
    208.     Vector3 ProjectOnContactPlane (Vector3 vector) {
    209.         return vector - contactNormal * Vector3.Dot(vector, contactNormal);
    210.     }
    211.  
    212.     float GetMinDot (int layer) {
    213.         return (stairsMask & (1 << layer)) == 0 ?
    214.             minGroundDotProduct : minStairsDotProduct;
    215.     }
    216.  
    217. }
    218.  

    If you need more informations please tell me :)
     
  2. Not really. You have code in the OnValidate method which supposed to be in the Awake method and you're completely missing the part where you're instantiating the PlayerActions.
    This is from the tutorial:
    screenshot1.png
     
  3. resteqs

    resteqs

    Joined:
    Nov 17, 2019
    Posts:
    7
    Dear Lurking-ninja i guess you have better eyes then me... i clearly oversaw the inputAction = new PlayerInputActions();
    Also putting the code in OnValidate() had no impact for my results.
     
  4. That is factually not true. You shouldn't put one-off things in methods which called multiple times. Especially not in the editor. Do not put initialization code in the OnValidate like that. Only if you check the parameters edited in the inspector and even there I strongly oppose making default assignments in OnValidate. Give feedback, error, warning, whatever, but don't change the variables.
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnValidate.html