Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Hikari · Apr 30, 2010 at 10:10 PM · instantiateraycasthit

Raycast + Instantiate

Hello there,

This script is called "Raymouse" and it's inside the main camera:

public var turretModel: GameObject;

function Update () {

 var ray: Ray = camera.ScreenPointToRay (Input.mousePosition);
 //Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
 var raycastHit: RaycastHit;

 if(Physics.Raycast(transform.position, ray.direction, raycastHit)){
 //print("RaycastHit " + raycastHit.collider.gameObject.name);   

 if(raycastHit.collider.gameObject.name=="base"){
     if(Input.GetKeyDown("mouse 0")){
         var newTurret: GameObject;

         newTurret = Instantiate(turretModel, raycastHit.collider.gameObject.transform.position, raycastHit.collider.gameObject.transform.rotation);

         Destroy(raycastHit.collider.gameObject);

         }
 }

 }

}

It instantiates an object when the user clicks a certain game object. But, depending on the order in which the objects are instantiated, it blocks when you try to instantiate something behind. I have attached a picture which shows what I just meant:

alt text

For it to work, I have to instantiate the ones in the back first. But it would be good if it worked in any order.

Comment
Add comment · Show 4
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 Hikari · Apr 30, 2010 at 10:17 PM 0
Share

One thing I forgot to add... These cylinders have a huge sphere collider around them, that may be stopping the ray from getting to the cubes that need to colide with the ray.

What would be a good solution...? I'd greatly appreciate help...

avatar image jester · Apr 30, 2010 at 10:29 PM 2
Share

i'll add this as a comment since you answered this yourself. your ray casting is hitting the giant sphere colliders. why do they need to be giant? if they absolutely need to be, i believe there's another raycast function that will return an array of everything the ray hits and you can iterate that and ignore the sphere colliders and only process the hits that make sense for instantiating new objects.

avatar image jester · Apr 30, 2010 at 10:30 PM 1
Share

here's the return all hits function: http://unity3d.com/support/documentation/ScriptReference/Physics.RaycastAll.html

avatar image Hikari · May 01, 2010 at 03:26 PM 0
Share

Jester, thank you very much for the help. I will look into it.

I tried to follow spinaljack's advice and give you the up vote, but I cuoldn't find that function. I hope my gratitude will suffice.

2 Replies

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

Answer by spinaljack · May 01, 2010 at 09:24 AM

You should give jester the up vote as he pretty much answered your question. Also, if you don't need to click on the blocks again after setting them you can change them to the ignore rays layer

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

Answer by VegaVG · Nov 07, 2012 at 11:16 PM

Another solution might be using capsule colliders instead of sphere colliders, if you are using cylinders

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

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

1 Person is following this question.

avatar image

Related Questions

Cube instantiaes on wrong side of the plane 1 Answer

Is it possible to adjust an Objects pivot without affecting any of the child objects? 1 Answer

Position before the camera? 2 Answers

Raycasthit and normal orientation 1 Answer

Instantiate an empty on a local raycast hit position 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