Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by el-mas-pro-470 · Jan 17, 2019 at 12:33 AM · raycastshootingnot workingheadshot

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);
     }
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Pineapple · Jan 17, 2019 at 11:39 AM 1
Share

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.

avatar image sh_code · Jan 17, 2019 at 12:57 PM 0
Share

the problems i think might cause this (btw yes, the codesnippet you provided is not enough to answer the question):

  1. 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

  2. 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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ray2yar · Jan 21, 2019 at 02:44 PM

Code for the raycasting?

Code for determining the head shot vs all others?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image el-mas-pro-470 · Jan 21, 2019 at 05:05 PM 0
Share
 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);
             }
avatar image WainWork el-mas-pro-470 · Jan 23, 2019 at 08:05 AM 0
Share

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.

avatar image
0

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);
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

147 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges