Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Radon · Jul 17, 2012 at 01:31 AM · javascriptdepth

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; }

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.
Comment
Add comment · Show 1 · 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
avatar image Radon · Jul 17, 2012 at 02:54 AM 0
Share

Ok, so I kind of got confused at first, but then I organized it right and it worked! Thanks a lot man.

avatar image
0

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.

Comment
Add comment · Show 1 · 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
avatar image Radon · Jul 17, 2012 at 02:08 AM 0
Share

how to do that ?

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


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