Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem with canMove variable

Discussion in 'Scripting' started by DustyShinigami, Jul 29, 2019.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    I'm completely stumped with this... Every other case where I've stopped the player's movement works fine and it's as it should be. His speed is basically set to 0 and his idle animation plays when canMove is false. However, in my current situation, when I set it to false, instead of just being idle, he keeps jumping on the spot until it becomes true again. I'm in the process of trying to disable the player movement when the screen fades to black and when it fades out again, he can move. I can't figure out why he jumps on the spot though. The variable has been made static, as I want to reference it in other scripts, such as being able to disable movement when he talks to an NPC. It works correctly there. It even works correctly during a couple of coroutines when the player picks something up or opens something.

    Not sure if anyone can see something amiss in my PlayerController that I can't...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public Animator anim;
    9.     public float moveSpeed;
    10.     public float jumpForce;
    11.     public bool jumped;
    12.     public bool allowInteract = false;
    13.     public float gravityScale;
    14.     public float knockBackForce;
    15.     public float knockBackTime;
    16.     //public Material textureChange;
    17.     //public Material textureDefault;
    18.     public bool allowCombat;
    19.     public bool allowJump;
    20.     public static bool canMove;
    21.     public ChestTrigger chest
    22.     {
    23.         get
    24.         {
    25.             if (_chest != null)
    26.                 return _chest;
    27.  
    28.             _chest = GameObject.FindWithTag("Chest Trigger")?.GetComponent<ChestTrigger>();
    29.             return _chest;
    30.         }
    31.     }
    32.  
    33.     private ChestTrigger _chest = null;
    34.     private Vector2 moveDirection;
    35.     private Vector2 moveHorizontal;
    36.     private float knockBackCounter;
    37.     private CharacterController controller;
    38.     private Quaternion targetRot;
    39.     private bool headingLeft = false;
    40.     private Pickup pickupWeapon;
    41.  
    42.     void Awake()
    43.     {
    44.         controller = GetComponent<CharacterController>();
    45.         anim = GetComponent<Animator>();
    46.     }
    47.  
    48.     void OnEnable()
    49.     {
    50.         ScreenFader.stopPlayer += CantMove;
    51.         ScreenFader.continueMovement += CanMove;
    52.     }
    53.  
    54.     void OnDisable()
    55.     {
    56.         ScreenFader.stopPlayer -= CantMove;
    57.         ScreenFader.continueMovement -= CanMove;
    58.     }
    59.  
    60.     void Start()
    61.     {
    62.         Cursor.visible = false;
    63.         pickupWeapon = FindObjectOfType<Pickup>();
    64.         targetRot = transform.rotation;
    65.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area"))
    66.         {
    67.             allowCombat = false;
    68.             allowJump = true;
    69.         }
    70.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
    71.         {
    72.             allowCombat = false;
    73.             allowJump = false;
    74.         }
    75.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area 2"))
    76.         {
    77.             allowCombat = false;
    78.             allowJump = true;
    79.         }
    80.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("level 1, room 1"))
    81.         {
    82.             allowCombat = true;
    83.             allowJump = true;
    84.         }
    85.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("level 1, room 2"))
    86.         {
    87.             allowCombat = true;
    88.             allowJump = true;
    89.         }
    90.     }
    91.  
    92.     void Update()
    93.     {
    94.         //To avoid any control or animation issues, each code block must be within this code block
    95.         if (knockBackCounter <= 0 && canMove)
    96.         {
    97.             moveHorizontal.x = Input.GetAxis("Horizontal");
    98.             //moveVertical.y = Input.GetAxis("Vertical");
    99.             moveDirection = new Vector2(moveHorizontal.x * moveSpeed, moveDirection.y);
    100.             controller.Move(moveDirection * Time.deltaTime);
    101.  
    102.             //Adds character rotation when changing direction horizontally
    103.             if ((moveHorizontal.x < 0f && !headingLeft) || (moveHorizontal.x > 0f && headingLeft))
    104.             {
    105.                 if (moveHorizontal.x < 0f) targetRot = Quaternion.Euler(0, 270, 0);
    106.                 if (moveHorizontal.x > 0f) targetRot = Quaternion.Euler(0, 90, 0);
    107.                 headingLeft = !headingLeft;
    108.             }
    109.             transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, Time.deltaTime * 20f);
    110.  
    111.             //Adds character rotation when changing direction vertically
    112.             /*if(moveVertical.y < 0f && lookingUp || (moveVertical.y > 0f && !lookingUp))
    113.             {
    114.                 if (moveVertical.y > 0f) targetrot = Quaternion.Euler(0, 0, 0);
    115.                 if (moveVertical.y < 0f) targetrot = Quaternion.Euler(0, 180, 0);
    116.                 lookingUp = !lookingUp;
    117.             }*/
    118.  
    119.             if (SceneManagement.insideHut && canMove)
    120.             {
    121.                 float moveHorizontalSnap = Input.GetAxis("Horizontal");
    122.                 float moveVerticalSnap = Input.GetAxis("Vertical");
    123.  
    124.                 //Adds character rotation when changing direction horizontally, but snaps instead of fully rotating
    125.                 if (moveHorizontalSnap > 0)
    126.                 {
    127.                     transform.eulerAngles = new Vector2(0, 90);
    128.                 }
    129.                 else if (moveHorizontalSnap < 0)
    130.                 {
    131.                     transform.eulerAngles = new Vector2(0, -90);
    132.                 }
    133.  
    134.                 //To possibly prevent diagonal movement with some control setups, try adding 'else if'
    135.                 //Adds character rotation when changing direction vertically, but snaps instead of fully rotating
    136.                 else if (moveVerticalSnap > 0)
    137.                 {
    138.                     transform.eulerAngles = new Vector2(0, 0);
    139.                 }
    140.  
    141.                 //Use this to make the character face towards the camera.
    142.                 /*else if (moveVertical < 0)
    143.                 {
    144.                     transform.eulerAngles = new Vector3(0, 180);
    145.                 }*/
    146.             }
    147.             if (controller.isGrounded)
    148.             {
    149.                 if (allowJump)
    150.                 {
    151.                     moveDirection.y = -1f;
    152.                     //GetKeyDown will require the player to press the button each time they want to jump. GetKey will allow the player to spam the jump button if they keep pressing it down.
    153.                     if (Input.GetKeyDown(KeyCode.KeypadPlus))
    154.                     {
    155.                         moveDirection.y = jumpForce;
    156.                         jumped = true;
    157.                     }
    158.                     else if (!Input.GetKeyDown(KeyCode.KeypadPlus))
    159.                     {
    160.                         jumped = false;
    161.                     }
    162.                     if (SceneManagement.xbox360Controller == 1)
    163.                     {
    164.                         if (Input.GetKeyDown("joystick button 0"))
    165.                         {
    166.                             moveDirection.y = jumpForce;
    167.                             jumped = true;
    168.                         }
    169.                         else if (!Input.GetKeyDown("joystick button 0"))
    170.                         {
    171.                             jumped = false;
    172.                         }
    173.                     }
    174.                     else if (SceneManagement.ps4Controller == 1)
    175.                     {
    176.                         if (Input.GetKeyDown("joystick button 1"))
    177.                         {
    178.                             moveDirection.y = jumpForce;
    179.                             jumped = true;
    180.                         }
    181.                         else if (!Input.GetKeyDown("joystick button 1"))
    182.                         {
    183.                             jumped = false;
    184.                         }
    185.                     }
    186.                 }
    187.             }
    188.             if (allowCombat)
    189.             {
    190.                 if (Input.GetKeyDown(KeyCode.Space))
    191.                 {
    192.                     anim.SetTrigger("Attack");
    193.                     //GameObject projectileObject = Instantiate(projectilePrefab);
    194.                     //projectilePrefab.transform.position = spawnPoint.transform.position + spawnPoint.transform.forward;
    195.                 }
    196.                 else if (SceneManagement.xbox360Controller == 1)
    197.                 {
    198.                     if (Input.GetKeyDown("joystick button 1"))
    199.                     {
    200.                         anim.SetTrigger("Attack");
    201.                     }
    202.                 }
    203.                 else if (SceneManagement.ps4Controller == 1)
    204.                 {
    205.                     if (Input.GetKeyDown("joystick button 2"))
    206.                     {
    207.                         anim.SetTrigger("Attack");
    208.                     }
    209.                 }
    210.             }
    211.             if (allowInteract)
    212.             {
    213.                 if (SceneManagement.xbox360Controller == 1)
    214.                 {
    215.                     if (Input.GetKeyDown("joystick button 2"))
    216.                     {
    217.                         StartCoroutine("Pickup");
    218.                     }
    219.                 }
    220.                 else if (SceneManagement.ps4Controller == 1)
    221.                 {
    222.                     if (Input.GetKeyDown("joystick button 0"))
    223.                     {
    224.                         StartCoroutine("Pickup");
    225.                     }
    226.                 }
    227.                 else
    228.                 {
    229.                     if (Input.GetKeyDown(KeyCode.Return))
    230.                     {
    231.                         StartCoroutine("Pickup");
    232.                     }
    233.                 }
    234.             }
    235.             if (ChestTrigger.allowOpen)
    236.             {
    237.                 if (Input.GetKeyDown(KeyCode.Return))
    238.                 {
    239.                     StartCoroutine("Open");
    240.                 }
    241.                 else if (Input.GetKeyDown(KeyCode.Return) && !ChestTrigger.allowOpen)
    242.                 {
    243.                     anim.SetBool("Interact", false);
    244.                 }
    245.                 else if (SceneManagement.xbox360Controller == 1)
    246.                 {
    247.                     if (Input.GetKeyDown("joystick button 2"))
    248.                     {
    249.                         StartCoroutine("Open");
    250.                     }
    251.                     else if (Input.GetKeyDown("joystick button 2") && !ChestTrigger.allowOpen)
    252.                     {
    253.                         anim.SetBool("Interact", false);
    254.                     }
    255.                 }
    256.                 else if (SceneManagement.ps4Controller == 1)
    257.                 {
    258.                     if (Input.GetKeyDown("joystick button 0"))
    259.                     {
    260.                         StartCoroutine("Open");
    261.                     }
    262.                     else if (Input.GetKeyDown("joystick button 0") && !ChestTrigger.allowOpen)
    263.                     {
    264.                         anim.SetBool("Interact", false);
    265.                     }
    266.                 }
    267.             }
    268.         }
    269.         else
    270.         {
    271.             knockBackCounter -= Time.deltaTime;
    272.         }
    273.         moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    274.  
    275.         anim.SetBool("isGrounded", controller.isGrounded);
    276.         //If the character can't move, then the Speed is set to 0. Otherwise it'll use the horizontal input value.
    277.         anim.SetFloat("Speed",
    278.             !canMove
    279.             ? 0f
    280.             : Mathf.Abs(Input.GetAxis("Horizontal")));
    281.  
    282.         //anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
    283.     }
    284.  
    285.     void CantMove()
    286.     {
    287.         canMove = false;
    288.     }
    289.  
    290.     void CanMove()
    291.     {
    292.         canMove = true;
    293.     }
    294.  
    295.     public IEnumerator Pickup()
    296.     {
    297.         canMove = false;
    298.         anim.SetBool("Interact", controller.isGrounded);
    299.         pickupWeapon.ObjectActivation();
    300.         yield return new WaitForSeconds(1f);
    301.         allowInteract = false;
    302.         canMove = true;
    303.     }
    304.  
    305.     public IEnumerator Open()
    306.     {
    307.         canMove = false;
    308.         anim.SetBool("Interact", controller.isGrounded);
    309.         chest.ChestOpen();
    310.         yield return new WaitForSeconds(1.5f);
    311.         canMove = true;
    312.     }
    313.  
    314.     public void Knockback(Vector3 direction)
    315.     {
    316.         knockBackCounter = knockBackTime;
    317.  
    318.         moveDirection = direction * knockBackForce;
    319.         moveDirection.y = knockBackForce;
    320.     }
    321. }