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 Borzi · Jun 01, 2013 at 05:22 PM · fpsparticlesfunctionraycasthit

How to Instantiate objects on Ray.Hit

Hey, I am working on a raycast system and whenever the particle instantiates, it seems to instantiate on the player. Anyone know why this is?

 function RayShoot ()
 {
 //Tells us if a the Ray has hit something
 var Hit : RaycastHit;
 //Direction of the Ray that the player has sent out
 var DirectionRay = transform.TransformDirection (Vector3.forward);
 
 //Make sure the player cannot shoot while reloading
 //Note: This only matters for ReloadTrigger as when
 //AutoReloading, "BulletsLeft" already is equal to 0
 if (IsReloading == false)
 {
     //Makes the Ray visible in Game inspector (for debugging perpouses)
     Debug.DrawRay(transform.position , DirectionRay * Range , Color.red);
     //If the Ray is sent out...
     if(Physics.Raycast(transform.position , DirectionRay , Hit, Range))
     {
         //...and we have hit a "RigidBody" Physics Object
         if(Hit.rigidbody)
         {
         //Make the object fly away by adding force to it
         Hit.rigidbody.AddForceAtPosition(DirectionRay * Force , Hit.point);
         //This finds the "ApplyDamage" function and does damage to the player/AI...Don't ask me
         //How this works :P
         Hit.collider.SendMessageUpwards("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
         //Tell the console we have hit and applied force to the object
         Debug.Log("Rigid Body has been Hit")
         }
         //If a RigidBody is not hit but an object with the Tag mudFloor...
         else if(Hit.transform.tag == "mudFloor")
         {
         //...create the Particle effect
         Instantiate(Particle1, transform.position, transform.rotation);
         //Tell the console that the particle is instantiated
         Debug.Log("Mud Particle Instantiated")
         }
         
     }
     //Take away from our BulletsLeft for eveytime we have fired
     BulletsLeft --;
     
     if(BulletsLeft < 0){
     //Make sure BulletsLeft cant go bellow "0"
     BulletsLeft = 0;
     }
     if(BulletsLeft == 0){
     //Automatic Reload if the Player is out of Ammo
     Reload();
     }
 }    
 }

Posting the whole function might not have been necessary, but I thought it would help to prevent confusion ^^ Thank you in advance!

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
3
Best Answer

Answer by robertbu · Jun 01, 2013 at 05:31 PM

You are using transform.position when you instantiate which is the position you are raycasting from. Use Hit.point.

 Instantiate(Particle1, Hit.point, transform.rotation);

You may or may not want transform.rotation for the rotation. Often the normal of the hit is used as part of this rotation to get the object to be aligned with the surface.

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 Borzi · Jun 01, 2013 at 07:33 PM 0
Share

Ah thanks, I thought this was the problem, but I didn't know how to change it. Transform.rotation does seem to do the job just perfectly in this case...

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

14 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

Related Questions

How can I make a Grapple Gun work with the Character Controller? 1 Answer

How to make enemy`s gun shoot directly on player and its allies from some distance. 1 Answer

what code should i be using if im only wanting a function to happen if a raycast does not return a hit 1 Answer

How i "import" variables? 2 Answers

Muzzleflash Particle Problem 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