- Home /
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...
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???
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.
Your answer
Follow this Question
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