TG9six 03a642d635 first push
first push
2025-09-06 17:17:39 +04:00

106 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
public class Projectile : MonoBehaviour
{
[Tooltip("The length of time before this gameobject is destroyed")]
private float maximumLifeTime = 3.0f;
private float destroyTime;
[SerializeField] private bool isGrenade = false;
[HideInInspector] public bool ownedByMainPlayer = false;
[SerializeField] private bool useGravity = false;
[SerializeField] private bool destroyOnImpact = true;
public bool rotatable = false;
[SerializeField] private float rotateSpeed;
private bool isRotating = true;
[HideInInspector]
public Vector3 direction;
[Tooltip("The speed of the projectile in m/s")]
public float thrust;
private Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
// Start is called before the first frame update
private void Start()
{
destroyTime = Time.time + maximumLifeTime;
rb.useGravity = useGravity;
rb.mass = 0.1f;
if (ownedByMainPlayer)
{
rb.AddForce(direction * thrust, ForceMode.Impulse);
}
}
public void ApplyForce(Vector3 direction, float thrustMultiplier)
{
rb.AddForce(direction * (thrust * thrustMultiplier), ForceMode.Impulse);
}
private void OnTriggerEnter(Collider other)
{
Player p = other.gameObject.GetComponentInParent<Player>();
if (p != null)
{
//send packet here
Debug.Log("proj hit");
PacketManager.sendProjecctileHit(gameObject.name);
}
// if (destroyOnImpact)
// {
// Destroy(gameObject);
// }
}
void OnCollisionEnter(Collision collision)
{
if (rotatable) { isRotating = false; }
if (destroyOnImpact)
{
Destroy(gameObject);
}
}
private void Update()
{
if (isRotating && rotatable)
{
transform.Rotate(new Vector3(90, 0, 0) * rotateSpeed * Time.deltaTime, Space.Self);
}
if (Time.time > destroyTime)
{
if (isGrenade && ownedByMainPlayer)
{
PacketManager.sendThrowableExplosion(transform.position.x, transform.position.y, transform.position.z);
Destroy(gameObject);
return;
}
NetworkPooler.Instance.PooledDestroy("Grenade", gameObject);
}
}
}