Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug My Turn Around method doesn't work in full screen mode

Discussion in 'Scripting' started by ManofDog, Jul 21, 2020.

  1. ManofDog

    ManofDog

    Joined:
    Nov 6, 2019
    Posts:
    9
    I'm trying to replicate Silent Hill movement, and I have a functional "TurnAround" Coroutine that I'm calling if the player isGrounded.

    Here's my movement:

    Code (CSharp):
    1.  
    2. private void CalculateMovement()
    3.     {
    4.          vertical = Input.GetAxis("Vertical");
    5.          horizontal = Input.GetAxis("Horizontal");
    6.  
    7.         Vector3 direction = new Vector3(0f, 0f, vertical);
    8.  
    9.         Vector3 movement = transform.TransformDirection(direction) * _moveSpeed;
    10.  
    11.         transform.Rotate(0f, horizontal * _turnSpeed * Time.deltaTime, 0f);
    12.         isGrounded = _characterController.SimpleMove(movement);
    13.     }
    14.  
    and here's were I'm calling it:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         CalculateMovement();
    4.  
    5.         if (Input.GetKey(KeyCode.LeftShift) && vertical > 0)
    6.         {
    7.             if (_rotation != null)
    8.             {
    9.                 StopCoroutine(_rotation);
    10.                 _rotation = null;
    11.             }
    12.             Sprint();
    13.         }
    14.  
    15.         else if (Input.GetKeyDown(KeyCode.Tab) && isGrounded)
    16.         {
    17.             _targetRotation = Quaternion.Euler(0, 180, 0) * transform.rotation;
    18.  
    19.             if(_rotation == null)
    20.             {
    21.                 _rotation = StartCoroutine(RotateToTargetOvertTime());
    22.             }      
    23.         }
    24.     }
    I'm noticing when in the Inspector that my public bool isGrounded keeps flickering. It doesn't keep me from rotating in small screen mode, but when I enter full screen mode I can't hardly rotate at all except randomly.

    What is it I'm missing?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    I'm guessing that the issue has to do with calling something physics-based (moving a controller, checking for a collision with the ground) while in Update() * - if the game frames and physics frames don't line up exactly right you can get inconsistent behavior. If that guess is accurate then the reason you're seeing different behavior between fullscreen and windowed is because there'll be different framerates.

    *I know the documentation has that in the example, I suspect the documentation is wrong. It doesn't make any sense for anything that collides to run in Update, and to check for being grounded, it has to do that.

    You may be able to compensate by giving yourself some leeway, and setting a "timer":
    Code (csharp):
    1. private float lastGroundedAt = -1f;
    2.  
    3. void Update() {
    4. ....
    5.     if (_characterController.SimpleMove(movement) ) {
    6.         lastGroundedAt = Time.time;
    7.     }
    8.     isGrounded = lastGroundedAt < Time.time + 0.05f;
    9. }
    This will make isgrounded true for a solid 0.05 seconds after it has been grounded, so it will at least be more consistent.
     
  3. ManofDog

    ManofDog

    Joined:
    Nov 6, 2019
    Posts:
    9
    I thought you didn't need to use FixedUpdate unless using RigidBody physics, or is that wrong? Would it be more efficient to just check if I'm touching Ground through OnTriggerEnter and change the bool that way?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Checking the CharacterController for isGrounded is a long time issue. Here's a nearly decade spanning thread on the topic.

    https://forum.unity.com/threads/controller-isgrounded-doesnt-work-reliably.91436/

    There's several people throwing out solutions of likely varying quality... but this one seems rather interesting:
     
  5. ManofDog

    ManofDog

    Joined:
    Nov 6, 2019
    Posts:
    9
    So what would be an efficient way to check if the player is grounded so I can prevent midair turning and other things?
     
  6. ManofDog

    ManofDog

    Joined:
    Nov 6, 2019
    Posts:
    9
    In case anybody is reading this in the future, I might have solved my issue by replacing:

    Code (CSharp):
    1. else if (Input.GetKeyDown(KeyCode.Tab) && isGrounded) .
    with:

    Code (CSharp):
    1.  raycastGrounded = Physics.Raycast(transform.position, Vector3.down, 1f);
    2.  
    3. else if (Input.GetKeyDown(KeyCode.Tab) && raycastGrounded) etc.
    and it seems to be working so far. Not sure if this is efficient or not but it's what I came up with.