Skip to content

Instantly share code, notes, and snippets.

@Mike-Schvedov
Created January 22, 2025 08:43
Show Gist options
  • Select an option

  • Save Mike-Schvedov/ce2c8b396da67641c4258f8df479a9e8 to your computer and use it in GitHub Desktop.

Select an option

Save Mike-Schvedov/ce2c8b396da67641c4258f8df479a9e8 to your computer and use it in GitHub Desktop.
Projectile (Survival 61 Episode)
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