PBG2H23ABR_PBG2H23AKL_PMC_P.../Plunderblock/Assets/Scripts/ScriptsKevin/BulletScript.cs

88 lines
2.6 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class BulletScript : MonoBehaviour
{
public DataBullet dataBullet;
2024-06-27 10:27:18 +02:00
private float damage;
private float speed;
private int ammo;
public GameObject particalClash;
2024-06-14 19:08:04 +02:00
public float lifetime = 3f;
private Rigidbody rb;
private Vector3 direction;
2024-06-14 16:58:09 +02:00
2024-06-14 19:08:04 +02:00
private float time = 0f;
2024-06-14 16:58:09 +02:00
private Ray ray;
private RaycastHit hit;
2024-06-14 19:08:04 +02:00
2024-06-28 11:24:09 +02:00
/// <summary>
/// Sobald life Time vorbei ist Destroy die Bullet.
/// </summary>
/// <param name="lifetime">Lifetime der Bullet</param>
2024-06-14 19:08:04 +02:00
private void die(float lifetime){
if(time > lifetime){
GameObject.Destroy(this.gameObject);
}
}
private void CheckForColliders(){
if(Physics.Raycast(ray, out hit)){ //wenn true wird durch out in variable hit ein RayCastHit gespeichert
direction = hit.point - transform.position;//Richtung in die, die Kugel fliegt
}
}
2024-06-28 11:24:09 +02:00
/// <summary>
/// clashPartical spawned clashPartical am EinschlagPunkt der Kugel.
/// </summary>
/// <param name="collision">Die Collision mit der Bullet.</param>
2024-07-03 12:46:41 +02:00
public void clashParticle(Collision collision){
ContactPoint contact = collision.GetContact(0);
Instantiate(particalClash,transform.position,Quaternion.LookRotation(contact.normal));
2024-06-27 10:27:18 +02:00
}
2024-06-28 11:24:09 +02:00
/// <summary>
/// Wenn die Bullet etwas behrührt zerstört sie sich selbst.
/// </summary>
/// <param name="collision">Die Collision mit der Bullet</param>
2024-06-27 10:27:18 +02:00
private void OnCollisionEnter(Collision collision){
Debug.Log(collision.gameObject.name + " UND HIER IST DER TAG: " + collision.gameObject.tag);
if(collision.transform.tag == "Enemy"){
2024-07-03 12:46:41 +02:00
collision.transform.GetComponent<EnemyHealth>().takeDamage(damage);
2024-06-27 10:27:18 +02:00
}
2024-07-03 12:46:41 +02:00
clashParticle(collision);
GameObject.Destroy(this.gameObject);
2024-06-27 10:27:18 +02:00
}
// Start is called before the first frame update
void Start()
{
2024-06-27 10:27:18 +02:00
damage = dataBullet.damage;
ammo = dataBullet.ammo;
speed = dataBullet.speed;
2024-06-14 19:08:04 +02:00
rb = this.GetComponent<Rigidbody>();
2024-07-03 12:46:41 +02:00
Physics.IgnoreCollision(GetComponent<Collider>(),GameObject.Find("Player").GetComponent<Collider>());
2024-06-27 10:27:18 +02:00
ray = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0f));
CheckForColliders();
}
// Update is called once per frame
void Update()
{
2024-06-27 10:27:18 +02:00
rb.velocity = direction.normalized * speed;
2024-06-14 19:08:04 +02:00
die(lifetime);
time += Time.deltaTime;
}
}