Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Reece2012FPS · Sep 25, 2012 at 07:14 AM · c#collisionfpsraycasting

Ultimate FPS Camera, Raycasting Troubles :(

When i shoot anything that has a collider, it creates a bullet hole and a nice effect, but this also happens when you shoot an enemy soldier, I cant work out how to make it so when you shoot a soldier you get a blood effect and when you shoot the ground or a wall you get a bullet hole.

Im pretty sure the answer lies within...

 if (m_BloodPrefab != null)
             {
                 Object.Instantiate(m_BloodPrefab, transform.position, transform.rotation);
             }

I have tried many different ways but none of them work :( Please Help

Bullet.c# Code...

 using UnityEngine;
 using System.Collections.Generic;
 
 [RequireComponent(typeof(AudioSource))]
 
 public class vp_Bullet : MonoBehaviour
 {
     
     // gameplay
     public float Range = 100.0f;            // max travel distance of this type of bullet in meters
     public float Force = 100.0f;            // force applied to any rigidbody hit by the bullet
     public float Damage = 1.0f;                // the damage transmitted to target by the bullet
     public string DamageMethodName = "Damage";    // user defined name of damage method on target
                                                 // TIP: this can be used to apply different types of damage, i.e
                                                 // magical, freezing, poison, electric
 
     public float m_SparkFactor = 0.5f;        // chance of bullet impact generating a spark
 
     // these gameobjects will all be spawned at the point and moment
     // of impact. technically they could be anything, but their
     // intended uses are as follows:
     public GameObject m_ImpactPrefab = null;    // a flash or burst illustrating the shock of impact
     public GameObject m_DustPrefab = null;        // evaporating dust / moisture from the hit material
     public GameObject m_SparkPrefab = null;        // a quick spark, as if hitting stone or metal
     public GameObject m_DebrisPrefab = null;    // pieces of material thrust out of the bullet hole and / or falling to the ground
     public GameObject m_BloodPrefab = null;        // pieces of material thrust out of the bullet hole and / or falling to the ground
     public GameObject m_BloodSoldier = null;
         
     // sound
     public List<AudioClip> m_ImpactSounds = new List<AudioClip>();    // list of impact sounds to be randomly played
     public Vector2 SoundImpactPitch = new Vector2(1.0f, 1.5f);    // random pitch range for impact sounds
 
 
     ///////////////////////////////////////////////////////////
     // everything happens in the Start method. the script that
     // spawns the bullet is responsible for setting its position 
     // and angle. after being instantiated, the bullet immediately
     // raycasts ahead for its full range, then snaps itself to
     // the surface of the first object hit. it then spawns a
     // number of particle effects and plays a random impact sound.
     ///////////////////////////////////////////////////////////
     void Start()
     {
         Ray ray = new Ray(transform.position, transform.forward);
         RaycastHit hit;
             
         // raycast against everything except the player itself and
         // debris such as shell cases
         if(Physics.Raycast(ray, out hit, Range, ~((1 << vp_Layer.Player) | (1 << vp_Layer.Debris))))
         {
 
             // move this gameobject instance to the hit object
             Vector3 scale = transform.localScale;    // save scale for 
             transform.parent = hit.transform;
             transform.localPosition = hit.transform.InverseTransformPoint(hit.point);
             transform.rotation = Quaternion.LookRotation(hit.normal);                // face away from hit surface
             if (hit.transform.lossyScale == Vector3.one)                            // if hit object has normal scale
                 transform.Rotate(Vector3.forward, Random.Range(0, 360), Space.Self);    // spin randomly
             else
             {
                 // rotated child objects will get skewed if the parent
                 // object has been unevenly scaled in the editor, so on
                 // scaled objects we don't support spin, and we need to
                 // unparent, rescale and reparent the decal.
                 transform.parent = null;
                 transform.localScale = scale;
                 transform.parent = hit.transform;
             }
 
             // if hit object has physics, add the bullet force to it
             Rigidbody body = hit.collider.attachedRigidbody;
             if (body != null && !body.isKinematic)
             {
                 Vector3 pushDir = ray.direction * Force;
                 body.AddForceAtPosition(pushDir, hit.point);
             }
 
             // spawn impact effect
             if (m_ImpactPrefab != null)
                 Object.Instantiate(m_ImpactPrefab, transform.position, transform.rotation);
 
             // spawn dust effect
             if (m_DustPrefab != null)
                 Object.Instantiate(m_DustPrefab, transform.position, transform.rotation);
 
             // spawn spark effect
             if (m_SparkPrefab != null)
             {
                 if (Random.value < m_SparkFactor)
                     Object.Instantiate(m_SparkPrefab, transform.position, transform.rotation);
             }
 
             // spawn debris particle fx
             if (m_DebrisPrefab != null)
             {
                 Object.Instantiate(m_DebrisPrefab, transform.position, transform.rotation);
             }
 
             // spawn blood effect
             if (m_BloodPrefab != null)
             {
                 Object.Instantiate(m_BloodPrefab, transform.position, transform.rotation);
             }
             
             // play impact sound
             if (m_ImpactSounds.Count > 0)
             {
                 audio.playOnAwake = false;
                 audio.minDistance = 3;
                 audio.maxDistance = 50;
                 audio.dopplerLevel = 0.0f;
                 audio.pitch = Random.Range(SoundImpactPitch.x, SoundImpactPitch.y);
                 audio.PlayOneShot(m_ImpactSounds[(int)Random.Range(0, (m_ImpactSounds.Count))]);
             }
 
             // do damage on the target
             hit.collider.SendMessage(DamageMethodName, Damage, SendMessageOptions.DontRequireReceiver);
 
             // if bullet is visible (i.e. has a decal), cueue it for deletion later
             if (gameObject.renderer != null)
                 vp_DecalManager.Add(gameObject);
             else
                 vp_Timer.In(1, TryDestroy);        // we have no renderer, so destroy object in 1 sec
         }
         else 
             Object.Destroy(gameObject);    // hit nothing, so self destruct immediately
     }
 
     ///////////////////////////////////////////////////////////
     // sees if the impact sound is still playing and, if not,
     // destroys the object. otherwise tries again in 1 sec
     ///////////////////////////////////////////////////////////
     private void TryDestroy()
     {
         if (!audio.isPlaying)
             Object.Destroy(gameObject);
         else
             vp_Timer.In(1, TryDestroy);
     }
 }
Comment
Add comment · Show 1
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 Fattie · Sep 25, 2012 at 07:19 AM 0
Share

what are you asking? do you know about the 'tag" system in Unity? it's likely you just want to use "tags" anc check if the tag is equal to whatever you're interested in. Alternately the "name" field may help you.

0 Replies

· Add your reply
  • Sort: 

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

10 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

Related Questions

How to make an ammo box script give ammo(change integers) in another script attached to a child of child of a collider. 0 Answers

How to render a lot of the same small objects 2 Answers

Distribute terrain in zones 3 Answers

2D Raycasting to detect if a player isGrounded 1 Answer

Reacting to terrain height changes without a rigidbody component 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