• 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
Question by G54 · Mar 16, 2011 at 02:25 AM · raycastinstantiaterandomspawn

Instantiate multiple objects and have them raycast

Hey everyone,

T$$anonymous$$s is my first forum post. I am rather new to unity and even newer to JavaScript t$$anonymous$$s is my second week with it. So basically to get to the point I've been playing around with a script that randomly adds an object wit$$anonymous$$n the boundaries of the playing field.

var floorHeight = -0.8f;// y axis var xAxis; var zAxis; var mine : GameObject; var spawn = 0;

 function Update () {

 w$$anonymous$$le (spawn <= 1)
 {
     var xAxis = Random.Range(-18,18);
     var zAxis = Random.Range(-17,21);
     //print(zAxis);
     Instantiate(mine, Vector3(xAxis, floorHeight,zAxis), Quaternion.identity);
     spawn++;
 }   

}

T$$anonymous$$s script works exactly as planned, but my playing field is uneven (a sea floor). So then I did some research and found out about somet$$anonymous$$ng called a ray cast. I found that you can get the distance with it so with some basic logic I figured that if i subtract the distance from the mines Y position it would be on top of the sea floor. Here the script.

var toGround = false; var obj : GameObject;

function Update () { if (toGround == true) { var $$anonymous$$t : RaycastHit; if (Physics.Raycast (obj.transform.position, -Vector3.down, $$anonymous$$t)) { Debug.DrawRay(transform.position, Vector3.down * 10, Color.green); var distanceToGround = $$anonymous$$t.distance; print(distanceToGround); var coll : Collider = obj.GetComponent(Collider); var bottom = coll.bounds.max.y; obj.transform.position.y = obj.transform.position.y - distanceToGround + bottom; toGround = false; } } }

Now the problem is they both work as there individual functions. One randomly spawns and one moves the mine. However the mines that are added through the RandomSpawn script do not get moved to the surface. I'm pretty stumped and any help would be much appreciate. Also the RandomSpawn script is attached to an empty gameobject and the raycast script is attached to the mines that are spawned in a prefab. Anyt$$anonymous$$ng that needs clarification please reply.

Thanks in advanced, Mike G.

Comment

People who like this

0 Show 1
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 G54 · Mar 16, 2011 at 06:46 PM 0
Share

Anyone have ideas? Any help at all is appreciated just point me in the right direction if you can.

-Mike G

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by G54 · Mar 17, 2011 at 10:11 PM

I figured it out!

if (Physics.Raycast (obj.transform.position, -Vector3.down, $$anonymous$$t))

I changed the above to the below

  if (Physics.Raycast (obj.transform.position, Vector3.down, $$anonymous$$t))

I'm not quite sure why it worked when the object was already there, but i assume that for the prefab the ray cast was going the wrong way! Hope t$$anonymous$$s helps others.

  • Mike G.

Comment

People who like this

0 Show 0 · 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.

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

Object Spawning Randomly 0 Answers

Why do objects keep spawning on top of one another? 0 Answers

Unity2d : Spawning in Prefabs 1 Answer

Random select from array and spawn 1 Answer

Creating a light in game C# 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