Advertisement
Atomic_Violetta

Incrementor and Collision

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