• 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 lampshade · Jan 11, 2011 at 11:41 PM · instantiatecollidersprefabs

Invisable Projectile; Instantiate at Collisions

Hi, is there any way that I can instantiate something only at game objects that have a collider attached to it? I don't want to see the prefab travel, just instantiate at a collider (a wall).

For example, if the player is facing toward a wall that is far away and I click the fire key (Input.GetAxis("Fire")) the prefab would instantiate at the collision (the wall)?

public class InstPrefabs : MonoBehaviour {

 private void InstThePreFab() {
     Vector3 fwd = transform.TransformDirection(Vector3.forward);
     if (Physics.Raycast(transform.position, fwd, Mathf.Infinity)) {
         Instantiate(prefab, transform.position, Quaternion.Identity);
     }
 }

}

This is kinda funky.

What I have, is when anything (both raycasts and projectiles) collide with something then it plays the collision sound (which is the sound attached to the prefab in the audiosource).

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
2

Answer by Jessy · Jan 12, 2011 at 12:13 AM

Raycast, and instantiate what you like at RaycastHit.point.

Comment
Add comment · Show 3 · 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 lampshade · Jan 12, 2011 at 01:51 AM 0
Share

can OnCollisionEnter be invoked when this raycast collides with a collider?

avatar image Jessy · Jan 12, 2011 at 03:41 AM 0
Share

No. I could see how you might think so, because a wheel collider utilizes raycasting, and it works for that, but no.

avatar image Statement · Jan 12, 2011 at 03:00 PM 2
Share

No, but you'll get the same sort of behaviour if you read my answer in the link I posted.

avatar image
0

Answer by Statement · Jan 12, 2011 at 12:32 AM

Check out this similar question. It contain full code in C# to make a RayCollider. With minor modifications you can ask it to test ray when pressing button. You can then have code such as

void OnRayCollision(RayCollision collision)
{
    // spawn prefab at collision.hit.point
}
Comment
Add comment · Show 11 · 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 lampshade · Jan 12, 2011 at 12:48 AM 0
Share

That doesn't exist.

avatar image Statement · Jan 12, 2011 at 09:30 AM 2
Share

Did you even bother to follow the link or read what I wrote?

avatar image Jessy · Jan 12, 2011 at 02:00 PM 1
Share

That's what you get for giving the men fish after I taught them how to make a rod and raise worms! :-D

avatar image Statement · Jan 12, 2011 at 02:55 PM 0
Share

Well, I see no big difference. Unity contain code. $$anonymous$$y answer contain code. If unity provide a method to check for collisions and I provide a method to check for collisions, what does it matter? Productivity should matter. But hey, it's nice to see you're describing yourself a $$anonymous$$cher and me a fish dealer.

avatar image Jessy · Jan 12, 2011 at 03:18 PM 0
Share

I don't think you understood what I said. What I'm saying, is that you keep giving more elaborate answers than I do (and in one case edited my answer to be so), and while I think your answers have looked more informative, that doesn't mean the asker is going to appreciate them more. $$anonymous$$y approach is to provide the smallest amount of direction I think would have helped me understand the solution to the question. I hardly know most askers here, so I can't really tailor the results to them, and there are other things I need to do with my time.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Managing gui with prefabs 2 Answers

changing scene vs. instantiated prefabs 1 Answer

Instantiate an object in a prefab? 1 Answer

How to destroy an instantiated prefab in C#? 4 Answers

Instantiate only certain objects within a prefab 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