Hi there. I was working on a system that would let enemy detect player when they shoot and then turn their way, but I can’t make it work for more than one player. If someone could explain the best way to do that, would be awesome. Basically, when player(one of them or both) shoots at enemy, they will detect which player shot and turn their way. Thanks in advance.
We’d have to know more about what your damage-dealing code looks like. For example, if your players shoot bullets, then your bullets will have to “remember” who shot them; if you just do a raycast when shooting, then it’s probably easier.
Raycast
That was more of a “show the code you have” not a “one word answer” sort of question.
Sorry, here
P.S Just the shooting part, the whole script is too long
private void ShootWeapon(int weapon)
{
if (!isAiming || isAimBlocked || behaviourManager.GetAnim.GetBool(reloadBool) || !weapons[weapon].Shoot())
{
return;
}
else
{
burstShotCount++;
behaviourManager.GetAnim.SetTrigger(shootingTrigger);
aimBehaviour.crosshair = shootCrosshair;
behaviourManager.GetCamScript.BounceVertical(weapons[weapon].recoilAngle);
Vector3 imprecision = Random.Range(-shotErrorRate, shotErrorRate) * behaviourManager.playerCamera.right;
Ray ray = new Ray(behaviourManager.playerCamera.position, behaviourManager.playerCamera.forward + imprecision);
RaycastHit hit = default(RaycastHit);
if (Physics.Raycast(ray, out hit, 500f, shotMask))
{
if (hit.collider.transform != this.transform)
{
DrawShoot(weapons[weapon].gameObject, hit.point, hit.normal, hit.collider.transform);
if (hit.collider.gameObject.GetComponent<HealthManager>())
{
hit.collider.gameObject.GetComponent<HealthManager>().TakeDamage(hit.point, ray.direction, weapons[weapon].bulletDamage);
}
}
}
else
{
Vector3 destination = (ray.direction * 500f) - ray.origin;
DrawShoot(weapons[weapon].gameObject, destination, Vector3.up, null, false, false);
}
AudioSource.PlayClipAtPoint(weapons[weapon].shotSound, gunMuzzle.position, 5f);
shotDecay = originalShotDecay;
isShotAlive = true;
}
}
I don’t see where you’re telling the enemy that is hit who shot at it. I’d probably pass a reference to the TakeDamage method which player fired the shot. Then use that reference as the target for the enemy to turn towards.
Well, yes, I didn’t tell the enemy who shot at him, since i didn’t know how to make him understand which player shot at him.
So on the enemy you’d have a reference to whatever player the enemy is targeting. In TakeDamage you can add a parameter which is a reference to the player which just shot the enemy. In the TakeDamage method you could add some logic for updating the reference to the target player.
That logic could go something like:
- If the existing player target reference is null, update with the new reference
- If the existing player target reference is further away than the new reference, update with the new reference
Then on the enemy, whenever the player target reference isn’t null and is within a certain range then the enemy does whatever it does to attack that player. That’s probably how I would implement it at least.
I actually took your advice and it’s working. In this script, I just added “this.transform”
if (hit.collider.gameObject.GetComponent<HealthManager>())
{
hit.collider.gameObject.GetComponent<HealthManager>().TakeDamage(hit.point, ray.direction, weapons[weapon].bulletDamage, this.transform);
}
and in the other script “Transform player”
public virtual void TakeDamage(Vector3 location, Vector3 direction, float damage, Transform player)
{
this.transform.LookAt(player);
}
And now, It seems like it’s working. So simple, yet I managed not to see this… Might work on this a bit more later tho… Anyways, Thank!