Advertisement
nstruth2

Need Help with Flying Plane. Collisions Recognized

Apr 11th, 2024
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | Source Code | 0 0
  1. Can't fly plane using book code. Here's the code:
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UI;
  7.  
  8. public class WorkingCodeP162 : MonoBehaviour
  9. {
  10.     bool startDeleteMessage;
  11.     float timer;
  12.     int score;
  13.     int nbPetrolCansCollected;
  14.     public GameObject plane;
  15.     // Start is called before the first frame update
  16.     void Start()
  17.     {
  18.         nbPetrolCansCollected = 0;
  19.         timer = 0.0f;
  20.         startDeleteMessage = false;
  21.         if (GameObject.Find("plane") != null)
  22.         {
  23.             plane = GameObject.Find("plane");
  24.             plane.SetActive(false);
  25.         }
  26.     }
  27.  
  28.     // Update is called once per frame
  29.     void Update()
  30.     {
  31.         if (startDeleteMessage == true)
  32.         {
  33.             timer = timer+Time.deltaTime;
  34.             if(timer >= 2)
  35.             {
  36.                 GameObject.Find("userMessageUI").GetComponent<Text>().text = "";
  37.                 timer = 0.0f;
  38.                 startDeleteMessage = false;
  39.             }
  40.         }
  41.     }
  42.  
  43. void OnControllerColliderHit(ControllerColliderHit hit)
  44. {
  45.     if (hit.collider.gameObject.tag == "pick_me" || hit.collider.gameObject.tag == "petrol_can")
  46.     {
  47.         string label = hit.collider.gameObject.tag;
  48.         if (label == "petrol_can")
  49.         {
  50.             nbPetrolCansCollected = nbPetrolCansCollected + 1;
  51.    GameObject.Find("userMessageUI").GetComponent<Text>().text = "You collected " + nbPetrolCansCollected +  " Can(s)!";;
  52.             Destroy(hit.collider.gameObject); //Collecting the can
  53.         }
  54.         print("collision with "+ label);
  55.         Destroy(hit.collider.gameObject);
  56.         startDeleteMessage = true;
  57.     }
  58.     if (hit.collider.gameObject.tag == "plane")
  59.         {
  60.             if (nbPetrolCansCollected < 3)
  61.             {
  62.                 GameObject.Find("userMessageUI").GetComponent<Text>().text = "Sorry You Need 3 Cans to Fly the Plane";
  63.                 startDeleteMessage = true;
  64.             }
  65.             else
  66.             {
  67.                 GameObject.Find("userMessageUI").GetComponent<Text>().text = "Well done. You can now fly the plane and leave the island";
  68.                 Destroy(GameObject.Find("AircraftJet"));
  69.                 plane.SetActive(true);
  70.                 gameObject.SetActive(false);
  71.                 startDeleteMessage = true;
  72.             }
  73.         }
  74. }
  75.  
  76. }
  77.  
Tags: C# Unity plane
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement