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 G54 · Mar 16, 2011 at 02:25 AM · instantiateraycastrandomspawn

Instantiate multiple objects and have them raycast

Hey everyone,

This is my first forum post. I am rather new to unity and even newer to JavaScript this 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 within the boundaries of the playing field.

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

 function Update () {

 while (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++;
 }   

}

This script works exactly as planned, but my playing field is uneven (a sea floor). So then I did some research and found out about something 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 hit : RaycastHit; if (Physics.Raycast (obj.transform.position, -Vector3.down, hit)) { Debug.DrawRay(transform.position, Vector3.down * 10, Color.green); var distanceToGround = hit.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. Anything that needs clarification please reply.

Thanks in advanced, Mike G.

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

-$$anonymous$$ike G

1 Reply

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

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

I figured it out!

if (Physics.Raycast (obj.transform.position, -Vector3.down, hit))

I changed the above to the below

  if (Physics.Raycast (obj.transform.position, Vector3.down, hit))

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 this helps others.

  • Mike G.

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

No one has followed this question yet.

Related Questions

Stop platforms spawning inside player 1 Answer

Instantiate Prefab at random 1 Answer

Spawning objects at points within another object? 1 Answer

Find a random location, spawn an object there, do it again. 0 Answers

Unity2d : Spawning in Prefabs 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