Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by yvyv1000 · Jul 23, 2016 at 06:38 AM · unity 5scripting probleminstantiateraycast

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

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

1 Reply

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

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.

Comment
Add comment · Show 5 · 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 yvyv1000 · Jul 23, 2016 at 05:41 PM 0
Share

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

avatar image Arshia001 yvyv1000 · Jul 24, 2016 at 08:43 AM 0
Share

See my edit.

avatar image yvyv1000 · Jul 25, 2016 at 12:40 AM 0
Share

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

avatar image Arshia001 yvyv1000 · Jul 25, 2016 at 07:24 AM 0
Share

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.

avatar image yvyv1000 Arshia001 · Jul 25, 2016 at 12:20 PM 0
Share

thanks for the information I'll remember 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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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