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 /
  • Help Room /
avatar image
0
Question by dowak23 · Feb 15, 2016 at 05:05 AM · scripting probleminstantiateshootinghole

The bullet hole colliding with player and "Triggers"

Hi, I have a big problem with my bullet holes. When the player is shooting, gun instantiates the bullet hole on the point where raycasing is hitting and it is workin fine. But when the player is moving fast or jumping and shooting, those bullet holes instantiates sometimes on him and it does not look well.

Another problem is with triggers.

I have some objects on scene with Trigger collider and the bullets are colliding with it too. The result of it looks at least funny but it still a bug,

Can you help me solve this problem? Here are scripts :

Recoil script : using UnityEngine; using System.Collections;

 public class Recoil : MonoBehaviour {
 
     private float recoil = 0.0f;
     private float maxRecoil_x = 20f;
     private float maxRecoil_y = 20f;
     private float recoilSpeed = 2f;
     
     public void StartRecoil(float recoilParam, float maxRecoil_xParam, float recoilSpeedParam)
     {
         // in seconds
         recoil = recoilParam;
         maxRecoil_x = maxRecoil_xParam;
         recoilSpeed = recoilSpeedParam;
         maxRecoil_y = Random.Range(-1f,1f);
     }
 
   public  void recoiling()
     {     
             
             if (recoil > 0f)
             {
                 Quaternion maxRecoil = Quaternion.Euler(maxRecoil_x, maxRecoil_y, 0f);
                 // Dampen towards the target rotation
                 transform.localRotation = Quaternion.Slerp(transform.localRotation, maxRecoil, Time.deltaTime * recoilSpeed);
                 recoil -= Time.deltaTime;
             }
             else {
                 recoil = 0f;
                 // Dampen towards the target rotation
                 transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, Time.deltaTime * recoilSpeed / 2);
             }             
     }
 
     // Update is called once per frame
     void Update()
     {  
         recoiling();
     }
 }
 

shooting script : using UnityEngine; using System.Collections; using System.Threading;

 public class Shooting : MonoBehaviour
 {
     [Header("Gameobjects")]
     public GameObject ammo;
     private Ammo ammunition;
     public GameObject bullethole;
     public GameObject gun;
      public GameObject recoil;
      public Recoil recl;
     
    AudioSource shoots;
     //public GameObject fireaftershoot;
     [Header("speed and time")]
     public Ppauser pauzer;
     
     public float speed = 30f;
     public float timer = 0.0f;
     public float maxtimer = 0.5f;
 
 
 
     void Start()
     {
         
     ammunition = ammo.GetComponent<Ammo>();        
         Cursor.visible = true;
         Cursor.lockState = CursorLockMode.Locked;
         shoots = GetComponent<AudioSource>();
 
         recl = recoil.GetComponent<Recoil>();
     }    
            
     void FixedUpdate()
     {
         float randomrecoilauto = Random.Range(-5, -1);
         float randomrecoilsingle = Random.Range(-5, -1);
         Vector3 betterhole = new Vector3(0, 0, 0.00002f);
         timer += Time.deltaTime;
 
         if (Input.GetMouseButtonDown(0) && pauzer.paused == false && timer >= maxtimer && ammunition.ammoInclip != 0 && ammunition.semiOrAuto == false) // strzelam w trybie semi
         {
             recl.StartRecoil(0.2f, randomrecoilsingle, 20f); // start recoil
             ammunition.ammoInclip -= 1; // takes the ammo from clip
             shoots.Play();
             timer = 0.0f;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             Vector3 test = ray.origin;
 
             
 
             //Instantiate(fireaftershoot, transform.position, gun.transform.rotation);
             if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 10)) // obliczam punkt trafienia
             {
                 Debug.DrawLine(test, hit.point, Color.blue, 2);
                 GameObject hole = Instantiate(bullethole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject; //strzelam i tworze dziure po kuli
             }
             
         }
         else if (Input.GetMouseButton(0) && pauzer.paused == false && timer >= 0.1f && ammunition.ammoInclip != 0 && ammunition.semiOrAuto == true) //strzelam w trybie auto
         {
             recl.StartRecoil(0.2f, randomrecoilauto, 20f); // start recoil
             ammunition.ammoInclip -= 1; // takes the ammo from clip
             shoots.Play();
             timer = 0.0f;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             Vector3 test = ray.origin;
             
             //Instantiate(fireaftershoot, transform.position, gun.transform.rotation);
             if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 10))
             {
                 Debug.DrawLine(test, hit.point, Color.blue, 2);
                 GameObject hole = Instantiate(bullethole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
             }
             
         }       
     }
 }
 

I've already tried to fix it, but it didn't worked, anyway , here's the code of the "fixer":

 using UnityEngine;
 using System.Collections;
 
 public class BulletIgnoreCollision : MonoBehaviour {
 
     public GameObject player;
     public GameObject trigger1;
     public GameObject trigger2;
     public GameObject trigger3;
     public GameObject trigger4;
     Collider playercol;
     Collider trigger1col;
     Collider trigger2col;
     Collider trigger3col;
     Collider trigger4col;
     Collider bullethole;
     void Start ()
     {
         player = GameObject.FindGameObjectWithTag("Player") ;
         trigger1 = GameObject.FindGameObjectWithTag("Triggers");
         trigger2 = GameObject.FindGameObjectWithTag("Triggers2");
         trigger3 = GameObject.FindGameObjectWithTag("Triggers3");
         trigger4 = GameObject.FindGameObjectWithTag("Triggers4");
 
         playercol = player.GetComponent<Collider>();
         trigger1col = trigger1.GetComponent<Collider>();
         trigger2col = trigger2.GetComponent<Collider>();
         trigger3col = trigger3.GetComponent<Collider>();
         trigger4col = trigger4.GetComponent<Collider>();
 
         bullethole = GetComponent<Collider>();
 
     }
     
     // Update is called once per frame
     void Update ()
     {
         Physics.IgnoreCollision(playercol, bullethole );
         Physics.IgnoreCollision(trigger1col, bullethole);
         Physics.IgnoreCollision(trigger2col, bullethole);
         Physics.IgnoreCollision(trigger3col, bullethole);
         Physics.IgnoreCollision(trigger4col, bullethole);
     }
 }
 


Comment
Add comment
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Keep getting an error saying game object is missing but everything is working just fine just a spam of errors. 0 Answers

Procedurally generating cubes 1 Answer

Interesting Enemy AI issue 2 Answers

Accessing the script of an instantiated object 1 Answer

Instantiate a Prefab along the surface of a sphere. C# 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