Könnt ihr mir bei meinem Skript helfen?
Hallo, ich habe die letzten Stunden ein Skript programmiert, aber es funktioniert nicht. Mir wird GetComponent<ShootingAi> falsch angezeigt. Die letzten Male hat es noch funktioniert, aber jetzt nicht mehr.
Hier ist das Skript. Der Fehler ist bei // RayCast:
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using UnityEngine;
using TMPro;
public class GunSystem : MonoBehaviour
{
// Werte
public int damage;
public float timeBetweenShooting;
public float spread;
public float range;
public float reloadTime;
public float timeBetweenShots;
public int magazineSize;
public int bulletsPerTap;
public bool allowButtonHold;
private int bulletsLeft;
private int bulletsShot;
// Bool
bool shooting;
bool readyToShoot;
bool reloading;
// Referenz
public Camera fpsCam;
public Transform attackpoint;
public RaycastHit rayHit;
public LayerMask whatIsEnemy;
// Grafik
public GameObject muzzleFlash;
public GameObject bulletHoleGraphic;
public float camShakeMagnitude;
public float camShakeDuration;
public TextMeshProUGUI text;
private void Awake()
{
bulletsLeft = magazineSize;
readyToShoot = true;
}
private void Update()
{
MyInput();
text.SetText(bulletsLeft + "/" + magazineSize);
}
private void MyInput()
{
if (allowButtonHold)
{
shooting = Input.GetKey(KeyCode.Mouse0);
}
else
{
shooting = Input.GetKeyDown(KeyCode.Mouse0);
}
if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading)
{
Reload();
}
// Shoot
if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
{
bulletsShot = bulletsPerTap;
Shoot();
}
}
private void Shoot()
{
readyToShoot = false;
// Spread
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
// Calculate Direction with Spread
Vector3 direction = fpsCam.transform.forward + new Vector3(x, y, 0);
// RayCast
if (Physics.Raycast(fpsCam.transform.position, direction, out rayHit, range, whatIsEnemy))
{
Debug.Log(rayHit.collider.name);
if (rayHit.collider.CompareTag("Enemy"))
{
rayHit.collider.GetComponent<ShootingAi>().TakeDamage(damage);
}
}
// Graphics
Instantiate(bulletHoleGraphic, rayHit.point, Quaternion.Euler(0, 180, 0));
Instantiate(muzzleFlash,attackpoint.position, Quaternion.identity);
bulletsLeft--;
bulletsShot--;
Invoke("ResetShot", timeBetweenShooting);
if (bulletsShot > 0 && bulletsLeft > 0)
{
Invoke("Shoot", timeBetweenShots);
}
}
private void ResetShot()
{
readyToShoot = true;
}
private void Reload()
{
reloading = true;
Invoke("ReloadFinished", reloadTime);
}
private void ReloadFinished()
{
bulletsLeft = magazineSize;
reloading = false;
}
}
Ich komme echt nicht mehr weiter und bräuchte etwas Hilfe.
C Sharp,
Visual Studio,
Unity