- Home /
headshot do not working with raycast? help!
Hello, I'm doing the stealth system and I can not do the headshot, that is, I can kill it stealthily with a shot, my weapon shoots like Raycast and the enemy is a rigidbody with a collider that IF detects it, but I put a collider in the head to the enemy with the script (I assign 1 life to the collider of the head) and it does not work, does not detect it, does anyone know what the error is? Thank you
using UnityEngine;
using System.Collections;
public class Target : MonoBehaviour {
public GameObject EnemyGameObject;
public Transform EnemyObject;
public GameObject DeathTransform;
public float Health = 100f;
public float MaxHealth = 100f;
public GameObject EnemyCounterObject;
void Start()
{
}
void Update()
{
if(Health < MaxHealth)
{
EnemyGameObject.gameObject.GetComponent<Enemy>().Distancia = EnemyGameObject.gameObject.GetComponent<Enemy>().DetectedDistance;
EnemyGameObject.gameObject.GetComponent<Enemy>().LastDistance = EnemyGameObject.gameObject.GetComponent<Enemy>().DetectedLastDistance;
}
}
public void TakeDamage (float Amount)
{
Health -= Amount;
if (Health <= 0f)
{
EnemyCounterObject.gameObject.GetComponent<EnemyKillCounter>().EnemyKilled += 1;
Die();
}
}
void Die ()
{
Instantiate(DeathTransform, EnemyObject.position,EnemyObject.rotation);
Destroy(EnemyGameObject);
}
}
Hey there, it might be helpful if you provide the codesnippet that does the actual raycast/shooting. In your current code i can see nothing that really relates to your question.
the problems i think might cause this (btw yes, the codesnippet you provided is not enough to answer the question):
your raycast gets blocked by the inside of player's head (if it has any geometry), therefore it never even hits the enemy's head
your enemy's head collider is somewhere in the hierarchy so that you're not properly differentiating from the enemy as a whole being hit vs. his head specifically being hit.
I'd guess it's number 2, but without more information, there's no way to be sure.
Answer by JonPQ · Jan 21, 2019 at 07:40 PM
I'm guessing its where you have the Target component, compared to where you have the collider parented to. Make sure they are on the Same gameObject in the hierarchy. Stick a breakpoint on that line (Target target = hit.transform.GetComponent();) or log the result, to see if it gets that component.
Soln:- put them on the same object, or change GetComponent to GetComponentInParent, or GetComponentInChildren
Answer by ray2yar · Jan 21, 2019 at 02:44 PM
Code for the raycasting?
Code for determining the head shot vs all others?
RaycastHit hit;
if (Physics.Raycast(FPScam.transform.position, FPScam.transform.forward, out hit, Range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if(target != null)
{
target.TakeDamage(Damage);
}
if(hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * ImpactForce);
}
So, u do not detecting where u shooting. Just in "target". U must create a new collader for "headshot" and check if u got into it.
Answer by jazzy-rey · Jul 17, 2019 at 06:26 AM
i code this and work just fine for me in my Weapon Script using the camera position for the Raycast hit and using hit.collider.tag instead of hit.transform.tag that was my hour of headbreaker i hope this work for you just use it on your shooting function.
void Shoot() {
RaycastHit hit;
if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out hit, range))
{
if (hit.collider.tag == "Enemy")
{
// This is the enemy script call to use enemy health and damage function
Enemy enemy = hit.transform.GetComponent<Enemy>();
if (enemy != null)
{
// here we taking health away from the enemy body
enemy.TakeDamage(damage);
}
}
else if (hit.collider.tag == "Head")
{
// calliing the enemy script again
Enemy enemy = hit.transform.GetComponent<Enemy>();
// here we are colliding with the enemy head and taking 100 of health with HeadShot
enemy.TakeDamage(headshotDamage);
}
else
{
Debug.Log(hit.transform.name);
}
}
}
Your answer
Follow this Question
Related Questions
problem with scripting 1 Answer
Random Raycast inside crosshairs 1 Answer
Arrow not shooting the in right orientation 0 Answers
Raycast layer problem 1 Answer
Problem in Shooting Accuracy 0 Answers