Search Unity

Simple JetPack Script AKA "I've tried everything..."

Discussion in '2D' started by jbrobinsawn, Oct 18, 2019.

  1. jbrobinsawn

    jbrobinsawn

    Joined:
    Sep 11, 2019
    Posts:
    2
    Ok so to preface my code is no doubt terrible and unnecessary in parts but I'm still learning and efficiency is not my goal here but functionality (if possible) using what's currently in place.

    I have a JetPack object in the game which I have successfully made to (upon collision) attach to the player, instantiate effects and alter gravity/type of jump to my preferences and it all works great. My problem is that I have multiple JetPack objects within the game and want to make it so that the JetPack effect resets upon collision with a second object. This happens IF a current JetPack is not detected but if one is detected it doesn't "refill the gas" so to speak. Any suggestions please? I'm at a loss. Here's the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. [RequireComponent(typeof(Rigidbody2D))]
    7. public class PlayerMove : MonoBehaviour
    8. {
    9.     public float movementSpeed = 10f;
    10.  
    11.     public Transform feetPos;
    12.     public float checkRadius;
    13.     public LayerMask whatIsGround;
    14.     public float jumpForce;
    15.     public float jetPackTimeCounter;
    16.     public float jetPackTime;
    17.  
    18.     public GameObject jetPackSprite;
    19.     public GameObject jetPackParticles;
    20.  
    21.     private SpriteRenderer mySpriteRenderer;
    22.  
    23.     public float jetPackForce;
    24.     public bool jetPack;
    25.     public bool getJetPack;
    26.  
    27.  
    28.  
    29.     private Animator anim;
    30.     private bool isGrounded;
    31.  
    32.     float movement = 0f;
    33.  
    34.     Rigidbody2D rb;
    35.  
    36.     // Start is called before the first frame update
    37.     void Start()
    38.     {
    39.         rb = GetComponent<Rigidbody2D>();
    40.  
    41.         anim = GetComponent<Animator>();
    42.  
    43.         mySpriteRenderer = GetComponent<SpriteRenderer>();
    44.  
    45.         jetPackSprite.SetActive(false);
    46.  
    47.         jetPackParticles.SetActive(false);
    48.  
    49.     }
    50.  
    51.     // Update is called once per frame
    52.     void Update()
    53.     {
    54.  
    55.         isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
    56.  
    57.         if(movement > 0){
    58.             transform.eulerAngles = new Vector3(0, 0, 0);
    59.         }else if(movement < 0){
    60.             transform.eulerAngles = new Vector3(0, 180, 0);
    61.         }
    62.  
    63.         if(isGrounded == true && Input.GetKeyDown(KeyCode.Space)){
    64.             jetPackTimeCounter = jetPackTime;
    65.             rb.velocity = Vector2.up * jumpForce;
    66.             Debug.Log("1");
    67.         }
    68.  
    69.         if(Input.GetKeyUp(KeyCode.Space)){
    70.             jetPackParticles.SetActive(false);
    71.         }
    72.  
    73.         if(jetPackTimeCounter <= 0){
    74.             jetPackTimeCounter = jetPackTime;
    75.             jetPackSprite.SetActive(false);
    76.             getJetPack = false;
    77.             Debug.Log("2");
    78.         }
    79.            
    80.         if(getJetPack == true && jetPackTimeCounter > 0){
    81.         if(Input.GetKey(KeyCode.Space)){
    82.                 jetPackParticles.SetActive(true);
    83.             rb.velocity = Vector2.up * jetPackForce;
    84.                 jetPackTimeCounter -= Time.deltaTime;
    85.                 Debug.Log("3");
    86.             }
    87.      }
    88.  
    89.         if(getJetPack == true && jetPackTimeCounter > 0){
    90.             jetPackSprite.SetActive(true);
    91.             return;
    92.  
    93.         }
    94.  
    95.    }
    96.     private void FixedUpdate()
    97.     {
    98.         movement = Input.GetAxis("Horizontal") * movementSpeed;
    99.  
    100.         if(Input.GetKey(KeyCode.RightArrow)){
    101.             anim.SetBool("isRunning", true);
    102.         } else {
    103.             anim.SetBool("isRunning", false);
    104.         }
    105.  
    106.         Vector2 velocity = rb.velocity;
    107.         velocity.x = movement;
    108.         rb.velocity = velocity;
    109.     }
    110.  
    111. }
    112.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class JetPack : MonoBehaviour {
    7.  
    8.     public PlayerMove move_script;
    9.     private void OnTriggerEnter2D(Collider2D player)
    10.  
    11.     {
    12.         if(player.tag == "Player")
    13.         {
    14.             move_script.getJetPack = true;
    15.             OnDestroy();
    16.         }
    17.     }
    18.  
    19.     public void OnDestroy()
    20.     {
    21.         Destroy(gameObject);
    22.     }
    23. }
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    "refill the gas" maybe just change jetPackTimeCounter ?
    Code (CSharp):
    1. if(player.tag == "Player")
    2.         {
    3.             move_script.getJetPack = true;
    4.             move_script.jetPackTimeCounter = 10 //something float
    5.             OnDestroy();
    6.         }
     
    MisterSkitz likes this.
  3. jbrobinsawn

    jbrobinsawn

    Joined:
    Sep 11, 2019
    Posts:
    2
    Damn, it's always the simplest solution! Thank you.