Advertisement
nstruth2

Bug in Source Code but Can't Find It

Feb 8th, 2024
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Puck : MonoBehaviour
  6. {
  7.  
  8.     public delegate void GoalHandler();
  9.     public event GoalHandler OnGoal;
  10.  
  11.     public float deceleration;
  12.     public float startingHorizontalPosition;
  13.  
  14.     private Rigidbody puckRigidbody;
  15.  
  16.     // Start is called before the first frame update
  17.     void Start()
  18.     {
  19.         puckRigidbody = gameObject.GetComponent<Rigidbody> ();
  20.         ResetPosition (true);
  21.     }
  22.  
  23.     // Update is called once per frame
  24.     void FixedUpdate()
  25.     {
  26.         puckRigidbody.velocity = new Vector3 (
  27.             puckRigidbody.velocity.x * deceleration,
  28.             puckRigidbody.velocity.y * deceleration,
  29.             puckRigidbody.velocity.z * deceleration
  30.             );
  31.     }
  32.  
  33.     void OnCollisionEnter (Collision collision) {
  34.         if(collision.gameObject.tag == "Goal") {
  35.             if (OnGoal != null) {
  36.                 OnGoal();
  37.             } else {
  38.                 gameObject.GetComponent<AudioSource>().Play();
  39.             }
  40.         }
  41.     }
  42.  
  43.     public void ResetPosition (bool isLeft) {
  44.         transform.position = new Vector3 (
  45.             startingHorizontalPosition * (isLeft ? -1 : 1),
  46.             transform.position.y,
  47.             transform.position.z);
  48.             puckRigidbody.velocity = Vector3.zero;
  49.     }
  50.  
  51. }
  52.  
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement