Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

using System.Collections; using System.Collections.Generic; using UnityEngine; using…

  1. User

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

    public class MarioController : MonoBehaviour { public float moveSpeed = 5f; public float jumpForce = 5f; public float ladderClimbSpeed = 2f; public int maxHealth = 3; private int currentHealth; private bool grounded; private bool onLadder; private bool climbingLadder; private Vector2 direction; private Rigidbody2D rb2d; private new Collider2D collider; private Collider2D[] results; private int currentSceneIndex; private ScoreCounter scoreCounter;

    public float hammerDuration = 9f;
    
    private bool isHammerActive;
    private float hammerTimer;
    
    [Header("Effects")]
    [Tooltip("The effect to create when colliding with Hammer")]
    public GameObject hammerEffect;
    
    
    private void Awake()
    {
        rb2d = GetComponent<Rigidbody2D>();
        collider = GetComponent<Collider2D>();
        results = new Collider2D[4];
    }
    
    private void Start()
    {
        currentHealth = maxHealth;
        currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        scoreCounter = FindObjectOfType<ScoreCounter>();
    }
    
    private void CheckCollision()
    {
        grounded = false;
        onLadder = false;
    
        Vector2 size = collider.bounds.size;
        size.y += 0.1f;
        size.x /= 2f;
    
        int amount = Physics2D.OverlapBoxNonAlloc(transform.position, size, 0f, results);
    
        for (int i = 0; i < amount; i++)
        {
            GameObject hit = results[i].gameObject;
    
            if (hit.layer == LayerMask.NameToLayer("Platform"))
            {
                grounded = hit.transform.position.y < (transform.position.y - 0.5f);
            }
            else if (hit.layer == LayerMask.NameToLayer("Ladder"))
            {
                onLadder = true;
            }
        }
    }
    
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Barrel") && isHammerActive)
        {
            Destroy(collision.gameObject);
            scoreCounter.score += 300;
        }
        else if (collision.gameObject.layer == LayerMask.NameToLayer("Hammer"))
        {
            Destroy(collision.gameObject);
            ActivateHammer();
            if (hammerEffect != null)
            {
                Instantiate(hammerEffect, transform.position, transform.rotation, null);
            }
        }
        else if (collision.gameObject.layer == LayerMask.NameToLayer("Barrel"))
        {
            currentHealth--;
            ResetScene();
        }
        else if (collision.gameObject.layer == LayerMask.NameToLayer("Win"))
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M");
        }
    }
    
    private void ResetScene()
    {
        if (currentSceneIndex == 0)
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M2");
        }
        else if (currentSceneIndex == 1)
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M3");
        }
        else if (currentSceneIndex == 2)
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M");
        }
    }
    
    private void ActivateHammer()
    {
        isHammerActive = true;
        hammerTimer = hammerDuration;
    }
    
    private void ResetHammer()
    {
        isHammerActive = false;
    }
    
    private void Update()
    {
        CheckCollision();
    
        if (isHammerActive)
        {
            hammerTimer -= Time.deltaTime;
            if (hammerTimer <= 0)
            {
                ResetHammer();
            }
        }
    
        if (grounded && Input.GetButtonDown("Jump"))
        {
            rb2d.velocity = new Vector2(rb2d.velocity.x, jumpForce);
        }
    
        float movement = Input.GetAxis("Horizontal");
    
        if (grounded)
        {
            direction.y = Mathf.Max(direction.y, -1f);
            climbingLadder = false;
        }
    
        if (onLadder && Input.GetButton("Vertical"))
        {
            climbingLadder = true;
            float climbDirection = Input.GetAxis("Vertical");
            rb2d.velocity = new Vector2(0, climbDirection * ladderClimbSpeed);
            rb2d.gravityScale = 0f;
        }
        else if (climbingLadder)
        {
            rb2d.gravityScale = 1f;
            rb2d.velocity = new Vector2(rb2d.velocity.x, 0f);
            rb2d.constraints = RigidbodyConstraints2D.FreezePositionX;
        }
        else
        {
            rb2d.gravityScale = 1f;
            rb2d.constraints = RigidbodyConstraints2D.FreezeRotation;
        }
    
        rb2d.velocity = new Vector2(movement * moveSpeed, rb2d.velocity.y);
    
        if (movement > 0)
        {
            transform.localScale = new Vector3(1f, 1f, 1f);
        }
        else if (movement < 0)
        {
            transform.localScale = new Vector3(-1f, 1f, 1f);
        }
    }
    

    }

    edit this script so if the currentSceneIndex == 1 or currentSceneIndex == 2, the score will not be reset to 0 but instead remain what it was from the previous scene

  2. Assistant

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

    public class MarioController : MonoBehaviour { public float moveSpeed = 5f; public float jumpForce = 5f; public float ladderClimbSpeed = 2f; public int maxHealth = 3; private int currentHealth; private bool grounded; private bool onLadder; private bool climbingLadder; private Vector2 direction; private Rigidbody2D rb2d; private new Collider2D collider; private Collider2D[] results; private int currentSceneIndex; private ScoreCounter scoreCounter;

    public float hammerDuration = 9f;
    
    private bool isHammerActive;
    private float hammerTimer;
    
    [Header("Effects")]
    [Tooltip("The effect to create when colliding with Hammer")]
    public GameObject hammerEffect;
    
    
    private void Awake()
    {
        rb2d = GetComponent<Rigidbody2D>();
        collider = GetComponent<Collider2D>();
        results = new Collider2D[4];
    }
    
    private void Start()
    {
        currentHealth = maxHealth;
        currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        scoreCounter = FindObjectOfType<ScoreCounter>();
    
        if (currentSceneIndex != 1 && currentSceneIndex != 2)
        {
            scoreCounter.score = 0;
        }
    }
    
    private void CheckCollision()
    {
        grounded = false;
        onLadder = false;
    
        Vector2 size = collider.bounds.size;
        size.y += 0.1f;
        size.x /= 2f;
    
        int amount = Physics2D.OverlapBoxNonAlloc(transform.position, size, 0f, results);
    
        for (int i = 0; i < amount; i++)
        {
            GameObject hit = results[i].gameObject;
    
            if (hit.layer == LayerMask.NameToLayer("Platform"))
            {
                grounded = hit.transform.position.y < (transform.position.y - 0.5f);
            }
            else if (hit.layer == LayerMask.NameToLayer("Ladder"))
            {
                onLadder = true;
            }
        }
    }
    
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Barrel") && isHammerActive)
        {
            Destroy(collision.gameObject);
            scoreCounter.score += 300;
        }
        else if (collision.gameObject.layer == LayerMask.NameToLayer("Hammer"))
        {
            Destroy(collision.gameObject);
            ActivateHammer();
            if (hammerEffect != null)
            {
                Instantiate(hammerEffect, transform.position, transform.rotation, null);
            }
        }
        else if (collision.gameObject.layer == LayerMask.NameToLayer("Barrel"))
        {
            currentHealth--;
            ResetScene();
        }
        else if (collision.gameObject.layer == LayerMask.NameToLayer("Win"))
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M");
        }
    }
    
    private void ResetScene()
    {
        if (currentSceneIndex == 0)
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M2");
        }
        else if (currentSceneIndex == 1)
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M3");
        }
        else if (currentSceneIndex == 2)
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M");
        }
    }
    
    private void ActivateHammer()
    {
        isHammerActive = true;
        hammerTimer = hammerDuration;
    }
    
    private void ResetHammer()
    {
        isHammerActive = false;
    }
    
    private void Update()
    {
        CheckCollision();
    
        if (isHammerActive)
        {
            hammerTimer -= Time.deltaTime;
            if (hammerTimer <= 0)
            {
                ResetHammer();
            }
        }
    
        if (grounded && Input.GetButtonDown("Jump"))
        {
            rb2d.velocity = new Vector2(rb2d.velocity.x, jumpForce);
        }
    
        float movement = Input.GetAxis("Horizontal");
    
        if (grounded)
        {
            direction.y = Mathf.Max(direction.y, -1f);
            climbingLadder = false;
        }
    
        if (onLadder && Input.GetButton("Vertical"))
        {
            climbingLadder = true;
            float climbDirection = Input.GetAxis("Vertical");
            rb2d.velocity = new Vector2(0, climbDirection * ladderClimbSpeed);
            rb2d.gravityScale = 0f;
        }
        else if (climbingLadder)
        {
            rb2d.gravityScale = 1f;
            rb2d.velocity = new Vector2(rb2d.velocity.x, 0f);
            rb2d.constraints = RigidbodyConstraints2D.FreezePositionX;
        }
        else
        {
            rb2d.gravityScale = 1f;
            rb2d.constraints = RigidbodyConstraints2D.FreezeRotation;
        }
    
        rb2d.velocity = new Vector2(movement * moveSpeed, rb2d.velocity.y);
    
        if (movement > 0)
        {
            transform.localScale = new Vector3(1f, 1f, 1f);
        }
        else if (movement < 0)
        {
            transform.localScale = new Vector3(-1f, 1f, 1f);
        }
    }
    

    }

  3. User

    The score reset to 0 when moving from currentSceneIndex == 0 to currentSceneIndex == 1

  4. Assistant

    To prevent the score from resetting to 0 when moving from currentSceneIndex == 0 to currentSceneIndex == 1, you can add some code modifications to the script.

    First, declare a static variable to store the score before transitioning to the next scene:

    private static int previousScore = 0;
    

    Next, in the Start() method, check if the previous scene index is 0. If it is, assign the previousScore value to the score counter:

    private void Start()
    {
        currentHealth = maxHealth;
        currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        scoreCounter = FindObjectOfType<ScoreCounter>();
    
        if (previousSceneIndex == 0)
        {
            scoreCounter.score = previousScore;
        }
    }
    

    Lastly, in the ResetScene() method, update the previousScore and previousSceneIndex values before loading the next scene:

    private void ResetScene()
    {
        previousScore = scoreCounter.score;
        previousSceneIndex = currentSceneIndex;
    
        if (currentSceneIndex == 0)
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M2");
        }
        else if (currentSceneIndex == 1)
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M3");
        }
        else if (currentSceneIndex == 2)
        {
            SceneManager.LoadScene("_Scene_DonkeyKong25M");
        }
    }
    

    These modifications will ensure that the score is retained when transitioning from currentSceneIndex == 0 to currentSceneIndex == 1.

  5. User

    write out the entire script with the modifications but also make it the same when currentSceneIndex == 1 transfers to currentSceneIndex == 2 just like when currentSceneIndex == 0 transfers to currentSceneIindex == 1

  6. Assistant

    Certainly! Below is the modified script that retains the score when transitioning from currentSceneIndex == 0 to currentSceneIndex == 1, as well as when transitioning from currentSceneIndex == 1 to currentSceneIndex == 2:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class MarioController : MonoBehaviour
    {
        public float moveSpeed = 5f;
        public float jumpForce = 5f;
        public float ladderClimbSpeed = 2f;
        public int maxHealth = 3;
        private int currentHealth;
        private bool grounded;
        private bool onLadder;
        private bool climbingLadder;
        private Vector2 direction;
        private Rigidbody2D rb2d;
        private new Collider2D collider;
        private Collider2D[] results;
        private int currentSceneIndex;
        private ScoreCounter scoreCounter;
    
        public float hammerDuration = 9f;
    
        private bool isHammerActive;
        private float hammerTimer;
    
        private static int previousScore = 0;
        private static int previousSceneIndex = 0;
    
    
        [Header("Effects")]
        [Tooltip("The effect to create when colliding with Hammer")]
        public GameObject hammerEffect;
    
    
        private void Awake()
        {
            rb2d = GetComponent<Rigidbody2D>();
            collider = GetComponent<Collider2D>();
            results = new Collider2D[4];
        }
    
        private void Start()
        {
            currentHealth = maxHealth;
            currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
            scoreCounter = FindObjectOfType<ScoreCounter>();
    
            if (previousSceneIndex == 0 || previousSceneIndex == 1)
            {
                scoreCounter.score = previousScore;
            }
        }
    
        private void CheckCollision()
        {
            grounded = false;
            onLadder = false;
    
            Vector2 size = collider.bounds.size;
            size.y += 0.1f;
            size.x /= 2f;
    
            int amount = Physics2D.OverlapBoxNonAlloc(transform.position, size, 0f, results);
    
            for (int i = 0; i < amount; i++)
            {
                GameObject hit = results[i].gameObject;
    
                if (hit.layer == LayerMask.NameToLayer("Platform"))
                {
                    grounded = hit.transform.position.y < (transform.position.y - 0.5f);
                }
                else if (hit.layer == LayerMask.NameToLayer("Ladder"))
                {
                    onLadder = true;
                }
            }
        }
    
        private void OnCollisionEnter2D(Collision2D collision)
        {
            if (collision.gameObject.layer == LayerMask.NameToLayer("Barrel") && isHammerActive)
            {
                Destroy(collision.gameObject);
                scoreCounter.score += 300;
            }
            else if (collision.gameObject.layer == LayerMask.NameToLayer("Hammer"))
            {
                Destroy(collision.gameObject);
                ActivateHammer();
                if (hammerEffect != null)
                {
                    Instantiate(hammerEffect, transform.position, transform.rotation, null);
                }
            }
            else if (collision.gameObject.layer == LayerMask.NameToLayer("Barrel"))
            {
                currentHealth--;
                ResetScene();
            }
            else if (collision.gameObject.layer == LayerMask.NameToLayer("Win"))
            {
                SceneManager.LoadScene("_Scene_DonkeyKong25M");
            }
        }
    
        private void ResetScene()
        {
            previousScore = scoreCounter.score;
            previousSceneIndex = currentSceneIndex;
    
            if (currentSceneIndex == 0)
            {
                SceneManager.LoadScene("_Scene_DonkeyKong25M2");
            }
            else if (currentSceneIndex == 1)
            {
                SceneManager.LoadScene("_Scene_DonkeyKong25M3");
            }
            else if (currentSceneIndex == 2)
            {
                SceneManager.LoadScene("_Scene_DonkeyKong25M");
            }
        }
    
        private void ActivateHammer()
        {
            isHammerActive = true;
            hammerTimer = hammerDuration;
        }
    
        private void ResetHammer()
        {
            isHammerActive = false;
        }
    
        private void Update()
        {
            CheckCollision();
    
            if (isHammerActive)
            {
                hammerTimer -= Time.deltaTime;
                if (hammerTimer <= 0)
                {
                    ResetHammer();
                }
            }
    
            if (grounded && Input.GetButtonDown("Jump"))
            {
                rb2d.velocity = new Vector2(rb2d.velocity.x, jumpForce);
            }
    
            float movement = Input.GetAxis("Horizontal");
    
            if (grounded)
            {
                direction.y = Mathf.Max(direction.y, -1f);
                climbingLadder = false;
            }
    
            if (onLadder && Input.GetButton("Vertical"))
            {
                climbingLadder = true;
                float climbDirection = Input.GetAxis("Vertical");
                rb2d.velocity = new Vector2(0, climbDirection * ladderClimbSpeed);
                rb2d.gravityScale = 0f;
            }
            else if (climbingLadder)
            {
                rb2d.gravityScale = 1f;
                rb2d.velocity = new Vector2(rb2d.velocity.x, 0f);
                rb2d.constraints = RigidbodyConstraints2D.FreezePositionX;
            }
            else
            {
                rb2d.gravityScale = 1f;
                rb2d.constraints = RigidbodyConstraints2D.FreezeRotation;
            }
    
            rb2d.velocity = new Vector2(movement * moveSpeed, rb2d.velocity.y);
    
            if (movement > 0)
            {
                transform.localScale = new Vector3(1f, 1f, 1f);
            }
            else if (movement < 0)
            {
                transform.localScale = new Vector3(-1f, 1f, 1f);
            }
        }
    }
    

    This script will now retain the score when transitioning from currentSceneIndex == 0 to currentSceneIndex == 1, as well as when transitioning from currentSceneIndex == 1 to currentSceneIndex == 2.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.