Created
January 22, 2025 08:43
-
-
Save Mike-Schvedov/ce2c8b396da67641c4258f8df479a9e8 to your computer and use it in GitHub Desktop.
Projectile (Survival 61 Episode)
This file contains hidden or 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
| using System.Collections; | |
| using UnityEngine; | |
| public class Projectile : MonoBehaviour | |
| { | |
| private Rigidbody rb; | |
| private int arrowDamage = 25; | |
| private bool isStuck = false; // Flag to track if the arrow is stuck | |
| private void Start() | |
| { | |
| rb = GetComponent<Rigidbody>(); | |
| Destroy(gameObject, 5f); // Destroy the arrow after 5 seconds if it doesn't hit anything | |
| Collider arrowCollider = GetComponent<Collider>(); | |
| Collider bodyCollider = PlayerState.Instance.playerBody.transform.Find("Body").GetComponent<Collider>(); | |
| Collider playerCollider = PlayerState.Instance.playerBody.GetComponent<Collider>(); | |
| if (arrowCollider != null && bodyCollider != null) | |
| { | |
| Physics.IgnoreCollision(arrowCollider, playerCollider); | |
| Physics.IgnoreCollision(arrowCollider, bodyCollider); | |
| } | |
| } | |
| // This method is called when the arrow hits a collider | |
| private void OnCollisionEnter(Collision collision) | |
| { | |
| // Check if the arrow is not already stuck | |
| if (!isStuck && !collision.transform.CompareTag("Player")) | |
| { | |
| isStuck = true; // Mark the arrow as stuck | |
| // Stop the movement by setting the Rigidbody's velocity and angular velocity to zero | |
| rb.velocity = Vector3.zero; | |
| rb.angularVelocity = Vector3.zero; | |
| // Optionally, freeze the Rigidbody's movement and rotation completely | |
| rb.isKinematic = true; | |
| Debug.Log("Arrow stuck! :" + collision.transform.name); | |
| } | |
| if (collision.transform.GetComponent<Animal>()) | |
| { | |
| Animal animal = collision.transform.GetComponent<Animal>(); | |
| animal.TakeDamage(arrowDamage); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment