Bullet becomes bullet hole

In my project, I have gun. My bullet uses rigidbody and spawns at the barrel of the gun when I fire. My bullet bounces through walls , sometimes it goes through walls.

Well I know, in physics raycast it’s very simple:

public GameObject par;
public int damage;

void Update()
{
    RaycastHit hit;
    Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

    if (Input.GetMouseButtonDown(0))
    {
        if (Physics.Raycast(ray, out hit, 100))
        {
            GameObject particleClone = Instantiate(par, hit.point, Quaternion.LookRotation(hit.normal)) as GameObject;
            Destroy(particleClone, 2);
            hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
        }
    }
}

How do I do this in rigidbody?
Bullet becomes a particle system or a bullet hole image.

Should I use OnCollisionEnter() or OnTriggerEnter()?

How do I make it a bullethole image or a particle system?

2

Answers


  1. Ears Diamandis
    0 Votes

    I found this JavaScript in: RAYCAST, BULLET HOLES, AND RANDOM ARRAYS

    You take this and add to the Launcher of your weapon:


    var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes
    
    function Update ()
    {
    
       var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction
       var hit : RaycastHit;
       Debug.DrawRay(transform.position, fwd * 10, Color.green); //drays our raycast and gives it a green color and a length of 10 meters
    
       if(Input.GetButtonDown ("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10))
       { //when we left click and our raycast hits something
             Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); //then we'll instantiate a random bullet hole texture from our array and apply it where we click and adjust
       // the position and rotation of textures to match the object being hit
       }
    }
    
  2. Jayson Ash
    0 Votes

    Both OnCollisionEnter and OnTriggerEnter could be used; however, have a look at this explanation on the differences: What is the difference between OnCollisionEnter and OnTriggerEnter?

Please signup or login to answer this question.

Signup for an account and start participating in our site today!




Social Signup

You can also login with your Facebook, Twitter or Linkedin account

Log in with Facebook