Search Unity

READ DESCRIPTION: Why does this flying script keep looping its making me fly and unfly over and over

Discussion in 'Scripting' started by DavidTurn, Aug 8, 2019.

  1. DavidTurn

    DavidTurn

    Joined:
    Jul 29, 2019
    Posts:
    5
    This is a flying script that when you get the poop you get to fly but it keeps flying and unflying this code was originally with pressing an input to fly but i dont want them to press a button i want them to automattically fly when collide with object with the tag fly. I aready disabled this script and put a script in the object tagged fly so it enables this script when collide.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. // FlyBehaviour inherits from GenericBehaviour. This class corresponds to the flying behaviour.
    4. public class FlyBehaviour : GenericBehaviour
    5. {
    6.     public string flyButton = "Fly";              // Default fly button.
    7.     public float flySpeed = 4.0f;                 // Default flying speed.
    8.     public float sprintFactor = 2.0f;             // How much sprinting affects fly speed.
    9.     public float flyMaxVerticalAngle = 60f;       // Angle to clamp camera vertical movement when flying.
    10.     private bool poop = false;
    11.     private int flyBool;                          // Animator variable related to flying.
    12.     private bool fly = false;                     // Boolean to determine whether or not the player activated fly mode.
    13.     private CapsuleCollider col;                  // Reference to the player capsulle collider.
    14.  
    15.     // Start is always called after any Awake functions.
    16.     void Start()
    17.     {
    18.         // Set up the references.
    19.         flyBool = Animator.StringToHash("Fly");
    20.         col = this.GetComponent<CapsuleCollider>();
    21.         // Subscribe this behaviour on the manager.
    22.         behaviourManager.SubscribeBehaviour(this);
    23.     }
    24.     private void OnTriggerEnter(Collider other)
    25.     {
    26.         if (other.gameObject.tag == "Fly")
    27.         { poop = true; }
    28.     }
    29.     private void OnTriggerexit(Collider other)
    30.     {
    31.         if (other.gameObject.tag == "Fly")
    32.         {
    33.             poop = false;
    34.         }
    35.     }
    36.  
    37.         // Update is used to set features regardless the active behaviour.
    38.         void Update()
    39.     {
    40.         // Toggle fly by input, only if there is no overriding state or temporary transitions.
    41.         if (poop = true && !behaviourManager.IsOverriding() && !behaviourManager.GetTempLockStatus(behaviourManager.GetDefaultBehaviour))
    42.         {
    43.          
    44.             fly = !fly;
    45.          
    46.  
    47.             // Force end jump transition.
    48.             behaviourManager.UnlockTempBehaviour(behaviourManager.GetDefaultBehaviour);
    49.  
    50.             // Obey gravity. It's the law!
    51.             behaviourManager.GetRigidBody.useGravity = !fly;
    52.  
    53.  
    54.             // Player is flying.
    55.             if (fly)
    56.             {
    57.                 // Register this behaviour.
    58.                 behaviourManager.RegisterBehaviour(this.behaviourCode);
    59.             }
    60.             else
    61.             {
    62.                 // Set collider direction to vertical.
    63.                 col.direction = 1;
    64.                 // Set camera default offset.
    65.                 behaviourManager.GetCamScript.ResetTargetOffsets();
    66.  
    67.                 // Unregister this behaviour and set current behaviour to the default one.
    68.                 behaviourManager.UnregisterBehaviour(this.behaviourCode);
    69.             }
    70.         }
    71.  
    72.         // Assert this is the active behaviour
    73.         fly = fly && behaviourManager.IsCurrentBehaviour(this.behaviourCode);
    74.  
    75.         // Set fly related variables on the Animator Controller.
    76.         behaviourManager.GetAnim.SetBool(flyBool, fly);
    77.     }
    78.  
    79.  
    80.     // This function is called when another behaviour overrides the current one.
    81.     public override void OnOverride()
    82.     {
    83.         // Ensure the collider will return to vertical position when behaviour is overriden.
    84.         col.direction = 1;
    85.     }
    86.  
    87.     // LocalFixedUpdate overrides the virtual function of the base class.
    88.     public override void LocalFixedUpdate()
    89.     {
    90.         // Set camera limit angle related to fly mode.
    91.         behaviourManager.GetCamScript.SetMaxVerticalAngle(flyMaxVerticalAngle);
    92.  
    93.         // Call the fly manager.
    94.         FlyManagement(behaviourManager.GetH, behaviourManager.GetV);
    95.     }
    96.     // Deal with the player movement when flying.
    97.     void FlyManagement(float horizontal, float vertical)
    98.     {
    99.         // Add a force player's rigidbody according to the fly direction.
    100.         Vector3 direction = Rotating(horizontal, vertical);
    101.         behaviourManager.GetRigidBody.AddForce((direction * flySpeed * 100 * (behaviourManager.IsSprinting() ? sprintFactor : 1)), ForceMode.Acceleration);
    102.     }
    103.  
    104.     // Rotate the player to match correct orientation, according to camera and key pressed.
    105.     Vector3 Rotating(float horizontal, float vertical)
    106.     {
    107.         Vector3 forward = behaviourManager.playerCamera.TransformDirection(Vector3.forward);
    108.         // Camera forward Y component is relevant when flying.
    109.         forward = forward.normalized;
    110.  
    111.         Vector3 right = new Vector3(forward.z, 0, -forward.x);
    112.  
    113.         // Calculate target direction based on camera forward and direction key.
    114.         Vector3 targetDirection = forward * vertical + right * horizontal;
    115.  
    116.         // Rotate the player to the correct fly position.
    117.         if ((behaviourManager.IsMoving() && targetDirection != Vector3.zero))
    118.         {
    119.             Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
    120.  
    121.             Quaternion newRotation = Quaternion.Slerp(behaviourManager.GetRigidBody.rotation, targetRotation, behaviourManager.turnSmoothing);
    122.  
    123.             behaviourManager.GetRigidBody.MoveRotation(newRotation);
    124.             behaviourManager.SetLastDirection(targetDirection);
    125.         }
    126.  
    127.         // Player is flying and idle?
    128.         if (!(Mathf.Abs(horizontal) > 0.2 || Mathf.Abs(vertical) > 0.2))
    129.         {
    130.             // Rotate the player to stand position.
    131.             behaviourManager.Repositioning();
    132.             // Set collider direction to vertical.
    133.             col.direction = 1;
    134.         }
    135.         else
    136.         {
    137.             // Set collider direction to horizontal.
    138.             col.direction = 2;
    139.         }
    140.  
    141.         // Return the current fly direction.
    142.         return targetDirection;
    143.     }
    144. }
    145.  
    146.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Don't you want to set poop == false somewhere after you act on it?

    As long as poop is true and your other behavior conditions are true, flying will toggle on/off since that is in Update().

    If you're unsure about that horrible mess of booleans on line 41, break them out and assign them into separate boolean variables, then evaluate the variables. That also lets you print them to Debug.Log() to see their state easily before or after acting on them.
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Also, you're setting
    poop = true
    , not checking if
    poop == true
    .
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    GOOD EYE @Madgvox ... nailed it.

    OP: see the change above, which applies to line 41 above. But I still don't see where poop is set to false, but that might be okay.