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
0
Question by smirlianos · Oct 18, 2013 at 03:11 PM · raycastbulletraycasthitweaponhit

instantiate decal at raycastHit

Hello,

I made a script for a gun that shoots with raycast method. Now I need to Instantiate a plane as a bullet hole effect on the fired position. I tried with raycastHit but I didn't succeed.

Any help?

 #pragma strict
 var FireSound : AudioClip;
 var ReloadSound : AudioClip;
 var Muzzle : GameObject;
 var MuzzleSpawner : GameObject;
 var HoldingWeapon : GameObject;
 var Hole : GameObject;
 
 private var attributes : WeaponAttributes;
 private var counter : float;
 
 function Awake () {
     attributes = HoldingWeapon.GetComponent("WeaponAttributes");
 }
 
 function Update () {
     counter --;
     
     var fwd = transform.TransformDirection (Vector3.forward);
     var newPos = Vector3.forward * 10;
     
     Debug.DrawRay(transform.position, transform.forward * attributes.Range, Color.blue);
     if(Input.GetMouseButton(0) && counter <= 0)
     {
         counter = attributes.RateOfFire;
         
         var muzz = Instantiate(Muzzle, MuzzleSpawner.transform.position, transform.rotation);
         muzz.transform.parent = gameObject.transform;
         
         audio.PlayOneShot(FireSound);
         
         if (Physics.Raycast (transform.position, fwd, attributes.Range))
         {
             print ("We hit something!");
         }
     }
 }
Comment
Add comment · Show 8
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 Owen-Reynolds · Oct 18, 2013 at 03:51 PM 0
Share

"I tried with raycastHit but I didn't succeed": Where? The script above only prints Hit Something. $$anonymous$$ight be helpful if you ins$$anonymous$$d posted (only?) the part where you try to instantiate the plane at raycastHit.

Also might be helpful if you changed the question title to be more specific. $$anonymous$$aybe "instantiate decal at raycasthit."

That specific Q has also been asked here before, so you might be able to just google and find examples.

avatar image sdgd · Oct 18, 2013 at 03:54 PM 0
Share

I would make it where the ray hited it there you istantiate the new plane for 0.01 float away towards you

OR if you are more skilled you paint the texture there that's where I cannot help you as I'm not there yet, ...

avatar image smirlianos · Oct 19, 2013 at 11:13 AM 0
Share

I removed my try from the code 'cause it just made the code harder to understand, it didn't worked at all.

avatar image meat5000 ♦ · Oct 19, 2013 at 11:19 AM 0
Share

The raycast variable 'point' gives the actual world space point at which the ray hit the collider.

http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-point.html

avatar image smirlianos · Oct 19, 2013 at 11:24 AM 0
Share

So how do I instantiate it on the actual hit point?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by samtperrin · Oct 19, 2013 at 02:10 PM

Use something like:

 RaycastHit hit;
 if(Physics.Raycast(transform.position, fwd, out rayHit, attributes.Range))
 {
     Instantiate(texture, hit.point, Quaternion.FromToRotation(Vector3.forward, rayHit.normal));
 }

This will Instantiate a texture at the hit point of the Raycast.

Comment
Add comment · Show 2 · 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 Owen-Reynolds · Oct 19, 2013 at 03:19 PM 2
Share

Well, almost -- the raycast has to also have the hit variable in the parens (that's how you tell it "and also please write down extra hit data, in this variable.")

The way it is now, hit.point will always be just 000 (or a "var not assigned" error in C#, which is better.)

avatar image samtperrin · Oct 19, 2013 at 03:59 PM 0
Share

You are absolutely correct. Have edited the code. Thanks for pointing that out.

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

18 People are following this question.

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

Related Questions

Help With RayCasting 1 Answer

Raycast visible bullets 2 Answers

Unity | Raycast Hit Empty Object which has Objects as Children 1 Answer

Need a script for a gun that shoots bullet using raycast 2 Answers

Vehicle is vibrating on the edge of the ramp/surface when I get the normal of the surface 0 Answers


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