Atomic_Violetta

HeroScript

Mar 16th, 2024
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | Gaming | 0 0
  1. // Hero Script - Adventure
  2. // name
  3. using UnityEngine;
  4.  
  5. public class HeroScript : MonoBehaviour
  6. {
  7.     // Set my Speed. If There is no speed, you can't move.
  8.     [SerializeField] private float memberSpeed = 3.0f;
  9.  
  10.     // I still don't know. I'm assuming it calls the Sprite Renderer, but I don't know what null is.
  11.     private SpriteRenderer memberSpriteRenderer = null;
  12.  
  13.     // I STILL don't know. Rigid Body.
  14.     private Rigidbody2D memberRigidBody = null; // I don't know, I'll tell you later.
  15.  
  16.     // Green Key
  17.     private bool memberHasGreenKey = false;
  18.  
  19.     // Orange Key
  20.     private bool memberHasOrangeKey = false;
  21.  
  22.     // Master Key
  23.     private bool memberHasMasterKey = false;
  24.  
  25.     // Score
  26.     private int memberScore = 0;
  27.  
  28.     // Lance
  29.     private bool memberHasLance = false;
  30.  
  31.     // Sword
  32.     private bool memberHasSword = false;
  33.  
  34.  
  35.  
  36.     // Start is called before the first frame update
  37.     void Start()
  38.     {
  39.         // Get the rigid body that we added in Unity.
  40.         memberRigidBody = this.GetComponent<Rigidbody2D>();
  41.  
  42.          // Get the sprite renderer from Unity.
  43.         memberSpriteRenderer = this.GetComponent<SpriteRenderer>();
  44.  
  45.     }
  46.  
  47.     // Update is called once per frame
  48.     void Update()
  49.     {
  50.         // Which direction does the player want to go?
  51.         // Assume the player doesn't want to move, so no direction.
  52.  
  53.         Vector3 localWhichDirection;
  54.         localWhichDirection = Vector3.one;
  55.  
  56.         // Get movement input
  57.         float horizontalInput = Input.GetAxis("Horizontal");
  58.         float verticalInput = Input.GetAxis("Vertical");
  59.  
  60.         // Calculate movement direction
  61.         Vector3 movementDirection = new Vector3(horizontalInput, verticalInput);
  62.  
  63.         // Apply movement
  64.         memberRigidBody.velocity = movementDirection * memberSpeed;
  65.  
  66.        
  67.     }
  68.        
  69.     // Hero just collided with something.
  70.     void OnCollisionEnter2D(Collision2D localCollision)
  71.     {
  72.         // Debug
  73.         Debug.Log(localCollision);
  74.  
  75.         // What is the other object?
  76.         GameObject localOtherObject = localCollision.gameObject;
  77.         {
  78.  
  79.             // Is it a Green Key?
  80.             if (localOtherObject.name.StartsWith("Green Key"))
  81.             {
  82.                 // Gain Green Key.
  83.                 memberHasGreenKey = true;
  84.                 Destroy(localOtherObject);
  85.             }
  86.  
  87.             // Is it a Green Door?
  88.  
  89.             if (localOtherObject.name.StartsWith("Hero"))
  90.             {
  91.                 this.gameObject.SetActive(true);
  92.             }
  93.             else if (memberHasGreenKey)
  94.             {
  95.                 this.gameObject.SetActive(false);
  96.             }
  97.                    
  98.  
  99.             // Is it a Coin?
  100.             if (localOtherObject.name.StartsWith("Coin"))
  101.             {
  102.                 // Increase the score.
  103.                 memberScore ++;
  104.                 print(memberScore);
  105.                 Destroy(localOtherObject);
  106.             }
  107.  
  108.             // Is it an Orange Key?
  109.             if (localOtherObject.name.StartsWith("Orange Key"))
  110.             {
  111.                 // Gain Orange Key
  112.                 memberHasOrangeKey = true;
  113.             this.gameObject.SetActive(false);
  114.             }
  115.  
  116.             // Is it an Orange Door?
  117.  
  118.             if (localOtherObject.name.StartsWith("Hero"))
  119.             {
  120.                 memberHasOrangeKey = true;
  121.                 this.gameObject.SetActive(false);
  122.             }
  123.  
  124.             // Is it a Sword?
  125.             if (localOtherObject.name.StartsWith("Sword"))
  126.             {
  127.                 memberHasSword = true;
  128.                 Destroy(localOtherObject);
  129.             }
  130.  
  131.             // Is it a Bat?
  132.  
  133.             if (localOtherObject.name.StartsWith("Bat"))
  134.             {
  135.                 if (!memberHasSword)
  136.                 {
  137.                     // The Bat kills the Hero.
  138.                     this.gameObject.SetActive(false);
  139.                 }
  140.                 else if (memberHasSword)
  141.                 {
  142.                     // The Hero kills the Bat.
  143.                     localOtherObject.SetActive(false);
  144.  
  145.                 }
  146.             }
  147.                
  148.             // Is it a Dragon?
  149.  
  150.             if (localOtherObject.name.StartsWith("Dragon"))
  151.             {
  152.                 if (memberHasLance)
  153.  
  154.                 // The Hero slays the Dragon.
  155.                 localOtherObject.SetActive(false);
  156.             }
  157.             else
  158.             {
  159.                 // The Dragon slays the Hero.
  160.                 this.gameObject.SetActive(true);
  161.             }
  162.        
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment