Search Unity

Resolved HELP! Update and FixedUpdate not running when On Screen Stick is being used.

Discussion in 'Input System' started by Blueprint_, Apr 24, 2023.

  1. Blueprint_

    Blueprint_

    Joined:
    Apr 15, 2021
    Posts:
    2
    Hi. Thanks for reading.

    I am currently making a top down shooter. It's a mobile game so I'm trying out the new Input System for the first time. There is a module called On Screen Stick.

    Now; I have a simple Player Controller. Inside, there is 3 lines of code that rotates a GameObject towards the closest enemy. But whenever I'm using the joystick, (or the enemy is too close for some reason), the rotator isn't rotating. It's like Update and Fixed only runs whenever the Input System is not active (or the enemy is too close...).

    This is my script:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4.  
    5. public class PlayerControlScript : MonoBehaviour
    6. {
    7.     //public Vector2 MoveInput { get; private set; } = Vector2.zero;
    8.  
    9.     TouchControls _controls;
    10.     Vector2 _move;
    11.  
    12.     public Rigidbody2D _rb;
    13.     public float _speed;
    14.     public int _maxHp;
    15.     public int _hp;
    16.     public float _dashCooldownInSeconds;
    17.     public float _dashDurationInSeconds;
    18.     public float _dashSpeedMultiplier;
    19.  
    20.     public List<GameObject> _enemies = new List<GameObject>();
    21.     public GameObject _closestEnemy;
    22.     public float _closestDistance;
    23.     private Rigidbody2D _rotatorObjectRb;
    24.     private float _dashTimer;
    25.     private bool _isDashing;
    26.     private bool _canDash = true;
    27.    
    28.  
    29.     private void Awake()
    30.     {
    31.         _controls = new TouchControls();
    32.         _controls.PlayerCapsule.Dash.performed += ctx => Dash();
    33.         _controls.PlayerCapsule.Move.performed += ctx => _move = ctx.ReadValue<Vector2>();
    34.         _controls.PlayerCapsule.Move.canceled += ctx => _move = Vector2.zero;
    35.         _rb = gameObject.GetComponent<Rigidbody2D>();
    36.         _maxHp = 1250;
    37.         _hp = _maxHp;
    38.         _rotatorObjectRb = GameObject.Find("RotatorObject").GetComponent<Rigidbody2D>();
    39.         UpdateEnemyList();
    40.     }
    41.  
    42.     private void Update()
    43.     {
    44.         Vector3 _m = new Vector3(_move.x, _move.y,0).normalized * _speed * Time.deltaTime;
    45.         _rb.MovePosition(transform.position + _m);
    46.  
    47.         Vector2 _rotatorDir = (Vector2)_closestEnemy.transform.position - _rb.position;
    48.         float _angle = Mathf.Atan2(_rotatorDir.y, _rotatorDir.x) * Mathf.Rad2Deg - 90f;
    49.         _rotatorObjectRb.rotation = _angle;
    50.     }
    51.  
    52.     private void FixedUpdate()
    53.     {
    54.         _closestDistance = Mathf.Infinity;
    55.         foreach(GameObject enemyObj in _enemies)
    56.         {
    57.             float dist = Vector3.Distance(enemyObj.transform.position, transform.position);
    58.             if (dist < _closestDistance)
    59.             {
    60.                 _closestEnemy = enemyObj;
    61.                 _closestDistance = dist;
    62.             }
    63.         }
    64.  

    Here is a video showing the problem:
    https://imgur.com/a/9MZf6Eu