-
-
Save xjjon/032a5576fd9cc98002d52c66222cc136 to your computer and use it in GitHub Desktop.
Unity Buff System Solution for https://www.jonathanyu.xyz/2016/12/30/buff-system-with-scriptable-objects-for-unity/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Firepoint : MonoBehaviour | |
{ | |
public float rayCastDistance = 10f; | |
public LayerMask layerMask; // Assign the desired layer(s) in the Unity Inspector. | |
private EnemyHealth enemyHealth; | |
float damageOvertime = 5f; | |
SpeedBuff speedBuffScriptableObject; | |
private void Start() | |
{ | |
enemyHealth = FindObjectOfType<EnemyHealth>(); | |
} | |
private void OnDrawGizmos() | |
{ | |
// Draw a line to represent the raycast direction and distance in the scene view. | |
Gizmos.color = Color.red; // You can choose any color you like | |
Vector2 rayDirection = new Vector2(Mathf.Cos(transform.eulerAngles.z * Mathf.Deg2Rad), Mathf.Sin(transform.eulerAngles.z * Mathf.Deg2Rad)); | |
Gizmos.DrawRay(transform.position, rayDirection * rayCastDistance); | |
} | |
private void Update() | |
{ | |
float angle = transform.eulerAngles.z; | |
Vector2 rayDirection = new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)); // DYNAMIC ROTATION 2d SPACE | |
RaycastHit2D raycastHit2D = Physics2D.Raycast(transform.position, rayDirection, rayCastDistance, layerMask); //specific mask | |
if (raycastHit2D.collider != null) | |
{ | |
/*Debug.Log(“Hit ” + raycastHit2D.collider.gameObject.name); | |
BuffableEntity buffableEntity = raycastHit2D.collider.gameObject.GetComponent<BuffableEntity>(); | |
if (buffableEntity != null) | |
{ | |
var timedBuff = speedBuffScriptableObject.InitializeBuff(raycastHit2D.collider.gameObject); | |
buffableEntity.AddBuff(timedBuff); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment