- Home /
Help in object depth
Hello, I would like to know how I could make my bullet holes be above my terrain or any other object in the game. Because my bullets are kind of struggling to be on top of objects so they are fading in and out when I look around or move. The code I used for my bullet is:
var Spawn : Transform; var BulletSpeed : float = 1000; var ReloadTime : float = 2; var AmmoInMag : float = 30; var IsFullAuto = true; static var AmmoLeft = 0; static var ReloadTTime : float; static var IsReloading = false; private var CanFire = true; var FireRate = 0.1; var range = 100; var DirtImpact : Transform; var ConcImpact : Transform; var WoodImpact : Transform; var BulletBulletHole : Transform; var WaterImpact : Transform; var MetalImpact : Transform;
function Start () { AmmoLeft = AmmoInMag; ReloadTTime = ReloadTime;
}
function Update (){
if (Input.GetKeyDown(KeyCode.R)) { BroadcastMessage("ReloadAnim"); Reload(); }
if(IsFullAuto == false){ if(Input. GetButtonDown("Fire1")){ if(AmmoLeft > 0){ BroadcastMessage("FireAnim"); Fire(); } } } else{ if(Input. GetButton("Fire1")){ if(AmmoLeft > 0){ BroadcastMessage("FireAnim"); Fire(); } } }
if(AmmoLeft == 0) { Reload(); }
if(AmmoLeft < 0){ AmmoLeft = 0; } }
function Fire(){ if(CanFire == true && IsReloading == false){
var hit : RaycastHit; var fwd = Spawn.TransformDirection(Vector3.forward); Debug.DrawRay(Spawn.position, fwd);
CanFire = false; yield WaitForSeconds(FireRate); CanFire = true; AmmoLeft -= 1; audio.Play();
if(Physics.Raycast(Spawn.position,fwd,hit,range)){ if(hit.collider.tag == "Dirt"){ Instantiate(DirtImpact,hit.point,Quaternion.FromToRotation(Vector3.forward,hit.normal)); Instantiate(BulletBulletHole,hit.point,Quaternion.FromToRotation(Vector3.forward,hit.normal)); } if(hit.collider.tag == "Concrete"){ Instantiate(ConcImpact,hit.point,Quaternion.FromToRotation(Vector3.forward,hit.normal)); Instantiate(BulletBulletHole,hit.point,Quaternion.FromToRotation(Vector3.forward,hit.normal)); } if(hit.collider.tag == "Wood"){ Instantiate(WoodImpact,hit.point,Quaternion.FromToRotation(Vector3.forward,hit.normal)); Instantiate(BulletBulletHole,hit.point,Quaternion.FromToRotation(Vector3.forward,hit.normal)); } if(hit.collider.tag == "Water"){ Instantiate(WaterImpact,hit.point,Quaternion.FromToRotation(Vector3.forward,hit.normal)); } if(hit.collider.tag == "Metal") Instantiate(MetalImpact,hit.point,Quaternion.FromToRotation(Vector3.forward,hit.normal)); Instantiate(BulletBulletHole,hit.point,Quaternion.FromToRotation(Vector3.forward,hit.normal));
} } }
function Reload(){ CanFire = false; IsReloading = true; BroadcastMessage("ReloadAnim"); yield WaitForSeconds (ReloadTime); IsReloading = false; CanFire = true; AmmoLeft = AmmoInMag; }
Answer by aldonaletto · Jul 17, 2012 at 01:55 AM
You should shift the hit.point slightly in the hit.normal direction - this will avoid the flickering that happens when two textures are at the same position. You could also improve your code by caching the tag in a variable and calculating the position and rotation right after entering the if (Physics.Raycast...):
... if(Physics.Raycast(Spawn.position,fwd,hit,range)){ var hitTag = hit.collider.tag; // cache the tag in a variable // calculate a position slightly shifted out of the surface: var pos = hit.point+0.01*hit.normal; // calculate the necessary rotation: var rot = Quaternion.FromToRotation(Vector3.forward, hit.normal); // then instantiate things more easily: if(hitTag == "Dirt"){ Instantiate(DirtImpact,pos,rot); Instantiate(BulletBulletHole,pos,rot); } if(hitTag == "Concrete"){ Instantiate(ConcImpact,pos,rot); Instantiate(BulletBulletHole,pos,rot); } ...Caching the tag is important: the tag property is actually an integer, and each time you read it a string with the tag name is returned, what implied in 5 string allocations in your original code - this causes more frequent garbage collections, a slow internal house keeping operation that may cause "hicups" in your game.
Ok, so I kind of got confused at first, but then I organized it right and it worked! Thanks a lot man.
Answer by DaveA · Jul 17, 2012 at 01:39 AM
Sounds like Z-fighting. Instantiate those decals a slight offset above the surface. you have the hit normal, so move the position you instantiate at about .005 (one half centimeter) along that direction, see if that helps.
Your answer
Follow this Question
Related Questions
Move at mouse click 0 Answers
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
About "translating" js into C# 1 Answer
FindIndex in a List 1 Answer