- Home /
Bullethole doesn't instantiate
var Damage = 10;
var FireRate = 0.1F;
var NextFire = 0.0F;
var BulletRange = 200;
var Enemy : GameObject;
var GunFireSound : AudioClip;
var Clip = 30;
var TotalBullets = 300;
var ReloadTime = 3;
var BulletHole : Texture;
function Update() {
if(Input.GetButton("Fire1") && Time.time > NextFire && Clip > 0){
NextFire = Time.time + FireRate;
GetComponent.<AudioSource>().clip = GunFireSound;
GetComponent.<AudioSource>().time = 0.2f;
GetComponent.<AudioSource>().Play();
Rayshooting();
Clip --;
}
else if(Clip == 0){
Reload();
}
if(Input.GetKey(KeyCode.R) && Clip < 30) {
Reload();
}
}
function Rayshooting() {
Debug.DrawRay(transform.position,transform.forward * BulletRange);
var hit : RaycastHit;
Physics.Raycast(transform.position,transform.forward,hit,BulletRange);
if (Physics.Raycast(transform.position,transform.forward,hit,BulletRange)&&hit.transform.gameObject == Enemy){
hit.transform.SendMessage("DamageTaken", Damage, SendMessageOptions.DontRequireReceiver);
}
if (Physics.Raycast(transform.position,transform.forward,hit,BulletRange)&&hit.transform.gameObject.tag == "Buildings" || "Terrain"){
Debug.Log("Hit wall!");
Instantiate(BulletHole, hit.point, Quaternion.LookRotation(hit.normal));
}
}
function Reload() {
yield WaitForSeconds(ReloadTime);
var Swap = 0;
if(TotalBullets > 0){
if(Clip != 0) {
Swap = 30 - Clip;
Clip = Clip + Swap;
TotalBullets = TotalBullets - Swap;
}
else if(Clip == 0){
TotalBullets -= 30;
Clip += 30;
}
}
else {
Debug.Log("Out Of Ammo!");
}
}
this is my shooting script right now (the reloading part isn't totally done yet) but I'm trying to get a bullethole at the point of hit as you can see but the bullethole does not instantiate. the code however does return the Hit wall to the debug log. can someone tell me what's wrong because it's frustrating because I'm not getting any error whatsoever
thanks
Answer by Arshia001 · Jul 23, 2016 at 04:36 PM
You're raycsting thrice. You only need to do it once, and then you can cache the results. A raycast is a relatively costly operation to do more than once for no reason. Try:
bool bHit = Physics.Raycast(transform.position,transform.forward,hit,BulletRange);
if (bHit && ....)
{
....
}
else if (bHit && ....)
{
....
}
As for your instantiation problem, if your log says the wall was hit, maybe the decal is spawned with the wrong orientation? Check the hierarchy view to see if it was spawned, then locate it in the scene view and see what's happening with the object.
EDIT: Oh, now I see. Your bullet hole object is a texture. You can't directly spawn a texture into the world. You'll need to have a gameobject with a quad mesh and a mesh renderer and assign the texture to that object. Better yet, you can make it into a prefab. You can either change your code like this:
var BulletHole: GameObject;
Or you could do this (sorry it's in C#, I don't know UnityScript, nor am I planning to learn it):
Texture BulletHole;
Mesh DecalMesh;
...
var GO = new GameObject("Decal");
GO.transform.position = hit.point;
GO.transform.rotation = Quaternion.LookRotation(hit.normal);
GO.AddComponent<MeshFilter>().mesh = DecalMesh;
var Mat = new Material(Shader.Find("Unlit/Texture")); // or whatever shader you want to use for the decal
Mat.mainTexture = BulletHole;
GO.AddComponent<MeshRenderer>().material = Mat;
Unnecessarily complex, as you can see. The prefab is the way to go if you ask me.
Changed the raycasting with your bHit and worked like a charm even saw some performance increase to but my decal doesn't even spawn so thats where the problem is at
the C# was No problem I know how Both well enough to concert the C# o UnityScript
Your prefab solution worked and so did the complex one but as you said it isn't really nessesary so I'm going with the prefab
Thanks for the help
I should probably mention that I've used the second approach quite a few times as well. It's useful when you want to $$anonymous$$imize asset references, or you want to have something you can't mess up by being careless and editing the wrong prefab. However, under normal circumstances, a prefab is definitely the way to go.
Your answer
Follow this Question
Related Questions
Learn: Survival Shooter aim offset from mouse position 1 Answer
Raycast do not detect when distance is closer or further than the max distance 1 Answer
Creating a GameObject variable without instantiating it? 1 Answer
How can I instantiate one shot over another? 1 Answer
How to place blocks so they stick to a surface in any orientation? And stick to a grid? 1 Answer