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

48 lines
1.1 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : MonoBehaviour
{
public DataBullet dataBullet;
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
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
}
}
// Start is called before the first frame update
void Start()
{
2024-06-14 19:08:04 +02:00
rb = this.GetComponent<Rigidbody>();
ray = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0f));
CheckForColliders();
}
// Update is called once per frame
void Update()
{
rb.velocity = direction.normalized * dataBullet.speed;
2024-06-14 19:08:04 +02:00
die(lifetime);
time += Time.deltaTime;
}
}