Hi,
I’m currently working on a game and I need some help with detecting if a player send out a raycast or a ai did, because my idea is for the ai to fight the player for whoever can get the most amount of wins, I have current wins set to each object and code for once 25 kills are made it will send the player to a different scene saying “You win” or whatever, but I need help with detecting if a player sent out the last raycast or if a player did, just so I can determine who wins, here is the code all 3 scripts for you guys to look at.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Entity : MonoBehaviour
{
[SerializeField] float StartingHealth;
private float health;
public float Currentkills;
public float Health
{
get
{
return health;
}
set
{
health = value;
Debug.Log(health);
if (health <= 0f)
{
Health = StartingHealth;
Currentkills += 1;
}
if (Currentkills is 25)
{
SceneManager.LoadSceneAsync(0);
Cursor.lockState = CursorLockMode.None;
}
}
}
void Start()
{
Health = StartingHealth;
Currentkills = 0;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DAMAGEGUN : MonoBehaviour
{
public float Damage;
public float BulletRange;
private Transform PlayerCamera;
public void Start()
{
PlayerCamera = Camera.main.transform;
}
public void Shoot()
{
Ray gunRay = new Ray(PlayerCamera.position, PlayerCamera.forward);
if (Physics.Raycast(gunRay, out RaycastHit hitInfo, BulletRange))
{
if (hitInfo.collider.gameObject.TryGetComponent(out Entity enemy))
{
enemy.Health -= Damage;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
public class Damagegunforai : MonoBehaviour
{
public float Damage;
public float BulletRange;
public void Shoot()
{
Ray gunRay = new Ray(transform.position, transform.forward);
if (Physics.Raycast(gunRay, out RaycastHit hitInfo, BulletRange))
{
if (hitInfo.collider.gameObject.TryGetComponent(out Entity enemy))
{
enemy.Health -= Damage;
}
}
}
}