Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Mewada-Neil · Mar 12, 2015 at 04:30 PM · bullethole

Can anyone help me out with the script???

I am trying to make an fps game. The following script is attached to bullet which is instantiated by a gun (with GunScript). I wrote the code such that it instantiates a bullet hole on both side of a wall which is penetrated by it. But it only makes it on the front side and not on the other side... I know its not a big deal, but I want to know where the problem is.

 using UnityEngine;
 using System.Collections;
 
 public class BulletScript : MonoBehaviour
 {
     public GameObject bulletHole;            // GameObject (Plane) with a bulletHole Texture
     public GameObject impactEffect;          //GameObject with impactEffect particle emmitter
 
     private float hitCount = 0f;
     private float damagePoints;
     private float impactForce;
     private float maxHits;
     private bool hasHit = false;   // To check if bullet had done max hits
     private Vector3 oldPos;
     private Vector3 newPos;
     
     void Start ()
     {
         oldPos = transform.position;
         newPos = transform.position;
     }
 
     void Update ()
     {
         if (hasHit) {
             return;
         }
         newPos = transform.position;
         Vector3 dir = newPos - oldPos;
         float dist = dir.magnitude;
         RaycastHit[] hits = Physics.RaycastAll (oldPos, dir, dist);
 
         for (int i = 0; i < hits.Length; i++)
         {
             RaycastHit hit = hits[i];
 
             newPos = hit.point;
             OnHit(hit);
             if (hitCount >= maxHits)
             {
                 hasHit = true;
                 Destroy (gameObject);
                 break;
             }
         }
 
         RaycastHit hit2;
         if (Physics.Raycast(newPos, -dir, out hit2, dist))
         {
             if (!hasHit) {
                 OnBackHit(hit2);
             }
         }
 
     }
 
     public void SetUp(float[] info)            // Setup method is called from the GunScript
     {
         damagePoints = info [0];           //Ignore this function
         impactForce = info [1];
         maxHits = info[2];
 
     }
 
     public void OnHit(RaycastHit hit)
     {
         hitCount++;
         Vector3 contactPoint = hit.point;
         Quaternion holeRotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
         GameObject impactInstance = Instantiate (impactEffect, hit.point + 0.1f * hit.normal, Quaternion.FromToRotation(Vector3.up, transform.forward)) as GameObject;
         GameObject holeInstance = Instantiate (bulletHole, contactPoint, holeRotation) as GameObject;
 
         holeInstance.transform.parent = hit.transform;
 
         if (hit.rigidbody) {
             hit.rigidbody.AddForce(transform.forward * impactForce, ForceMode.Impulse);
         }
     }
 
     public void OnBackHit(RaycastHit hit)       //This method is used for creating bulletHole on other side of the penetrated wall
     {
         Vector3 contactPoint = hit.point;
         Quaternion holeRotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
 
         Instantiate (impactEffect, hit.point + 0.1f * hit.normal, Quaternion.FromToRotation (Vector3.up, transform.forward));
         GameObject newHole = Instantiate (bulletHole, contactPoint, holeRotation) as GameObject;
         newHole.transform.parent = hit.transform;
     }
 }


Thanks in advance...

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 Mewada-Neil · Mar 13, 2015 at 10:53 AM 0
Share

So I have to use raycast as soon as the bullet hits the first side and in same direction in which the bullet is travelling???

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Mar 12, 2015 at 04:38 PM

Your walls are cubes or planes or 3D models of some sort, right?

The built in Unity primitives, and almost every other 3D model, only has normals facing in one direction. This means that you can see their faces only when looking at them from one direction, not the other.

The same thing goes for their colliders - only things that come from the outside will collide with them, anything that starts out inside the object will move unnoticed through it.

I can see two solutions - either add planes on the inside of the walls that face inwards to have the collision be detected, or use raycasts after you have hit the wall from the front to find when your bullet has passed through on the other side.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Bullets holesnot instantiating in the correct position 0 Answers

Bullet holes in the wall (raycast) 1 Answer

bullet hole on shot 0 Answers

How do i destroy a particle when an object is destroyed 1 Answer

Change Texture of Clone 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