Disabling jump in a scene

I seem to be having issues disabling the jump option when my character enters another scene. I don’t understand why as I was able to disable the attack option in the same scene. I tried following the same setup as before, but it just won’t work. :-\ Can anyone point out where I’m going wrong or what I need to change? It’s the hut_interior I’m trying to disable it for. Thanks.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour
{
    public Animator anim;
    public float moveSpeed;
    public float jumpForce;
    public bool jumped;
    public bool attack;
    public static bool interact = false;
    public static bool allowInteract = false;
    public float gravityScale;
    public float knockBackForce;
    public float knockBackTime;
    public float invincibilityLength;
    public Renderer playerRenderer;
    public Material textureChange;
    public Material textureDefault;
    public bool allowCombat;
    public bool allowJump;
    public string startPoint;
    public bool notDestroyed;

    private Vector3 moveDirection;
    private Vector3 extraDirections;
    private float knockBackCounter;
    private float invincibilityCounter;
    private CharacterController controller;
    private static bool playerExists;

    void Start()
    {
        Cursor.visible = false;
        controller = GetComponent<CharacterController>();
        //Every bool starts on false
        if (!playerExists)
        {
            playerExists = true;
            DontDestroyOnLoad(transform.gameObject);
            notDestroyed = true;
        }
        else
        {
            Destroy(gameObject);
        }
        if (notDestroyed)
        {
            if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area"))
            {
                allowCombat = false;
                allowJump = true;
            }
            else if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
            {
                allowCombat = false;
                allowJump = false;
            }
            else if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("level 1"))
            {
                allowCombat = true;
                allowJump = true;
            }
        }
    }

    void Update()
    {
        if (knockBackCounter <= 0)
        {
            float moveHorizontal = Input.GetAxis("Horizontal");
            float moveVertical = Input.GetAxis("Vertical");
            moveDirection = new Vector3(moveHorizontal * moveSpeed, moveDirection.y);
            extraDirections = new Vector3(moveVertical * moveSpeed, extraDirections.y);

            if (moveHorizontal > 0)
            {
                transform.eulerAngles = new Vector3(0, 90);
            }
            else if (moveHorizontal < 0)
            {
                transform.eulerAngles = new Vector3(0, -90);
            }
            //To possibly prevent diagonal movement with some control setups, try adding 'else if'
            else if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
            {
                if (moveVertical > 0)
                {
                    transform.eulerAngles = new Vector3(0, 0);
                }
                else if (moveVertical < 0)
                {
                    transform.eulerAngles = new Vector3(0, 180);
                }
            }
            if (controller.isGrounded)
            {
                if (allowJump)
                {
                    moveDirection.y = -1f;
                    //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.
                    if (Input.GetKeyDown(KeyCode.KeypadPlus) || Input.GetKeyDown("joystick button 1"))
                    {
                        moveDirection.y = jumpForce;
                        jumped = true;
                    }
                    else if(!allowCombat && !Input.GetKeyDown(KeyCode.KeypadPlus) || !Input.GetKeyDown("joystick button 1"))
                    {
                        jumped = false;
                    }
                }

                if (allowCombat)
                {
                    if (Input.GetKey(KeyCode.Space) || Input.GetKey("joystick button 7"))
                    {
                        attack = true;
                        playerRenderer.material = textureChange;
                    }
                }
                else if (!allowCombat)
                {
                    attack = false;
                    playerRenderer.material = textureDefault;
                }

                if (allowInteract)
                {
                    if (SceneManagement.xbox360Controller == 1)
                    {
                        //if (Input.GetKeyDown("joystick button 2"))
                        {
                            interact = true;
                        }
                    }
                    else if (SceneManagement.ps4Controller == 1)
                    {
                        //if (Input.GetKeyDown("joystick button 0"))
                        {
                            interact = true;
                        }
                    }
                    else
                    {
                        interact = true;
                    }
                }
            }
        }
        else
        {
            knockBackCounter -= Time.deltaTime;
        }

        moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
        controller.Move(moveDirection * Time.deltaTime);

        anim.SetBool("isGrounded", controller.isGrounded);
        anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));

        if (attack)
        {
            anim.SetTrigger("Attack");
        }

        if (interact)
        {
            if (Input.GetKeyDown(KeyCode.Return))
            {
                anim.SetBool("Interact", controller.isGrounded);
                FindObjectOfType<Pickup>().ObjectActivation();
            }
        }
    }

    public void Knockback(Vector3 direction)
    {
        knockBackCounter = knockBackTime;

        moveDirection = direction * knockBackForce;
        moveDirection.y = knockBackForce;
    }
}

I’m not sure this approach will work:

if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area"))

You’re comparing scene objects and I’m not sure asking for it twice will give you the same thing.

Not only that you have created three fresh dependencies. If you rename a scene, you’ll instantly get bugs.

Instead, make two MonoBehaviors, one called CantJump and one called CantCombat. Once you make them, delete all functions inside of them, just make them empty classes. We’re only going to use them as markers.

Now in the scenes where you don’t want jumping or combat to happen, simply add a single GameObject and put the CantJump or CantCombat script on that object. You might want to actually name that GameObject something like “CantJump” or “CantCombat” so you can find it later easily!!

Now in your Start() just say instead:

// presume it's a free-for-all, cats and dogs jumping and combatting everywhere mostly
allowJump = true;
allowCombat = true;

// stay on the ground
if (FindObjectOfType<CantJump>()) allowJump = false;

// get along nicely please
if (FindObjectOfType<CantCombat>()) allowCombat = false;

And voila you have made self-documenting code and scenes, forever extensible for any other types of behavior you want to prescribe or proscribe.

1 Like

Thanks. That seems to work nicely. :slight_smile: I also experimented with another way - I tried putting the other scene object for allowJump within the Update function. I figured the first one - start_area - was working fine because that happens on play right at the start. As that wasn’t going to work for the other two, I deleted the one for level 1 until a bug relating to that is fixed, and put the hut_interior one in Update. That seems to work. :slight_smile: Not sure if that’s a ‘tidy’ or good way of doing things though…