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 Pixelen · Jan 07, 2012 at 08:05 PM · transformprefabsaxisvectorinstatiate

Instatiate randomly along a vector

Hi, I'm new to Unity and I'm trying to get prefabs to only instatiate randomly along the X axis/vector 3 but I have no idea how to go about it. This is the code I've got at the moment:

 var thePrefab : GameObject;
 
 function Instantiate () {
 
     var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
 
 }

This makes the prefabs instatiate in the same place as the empty object I put the script in, but I'd like them to only respawn randomly along the X axis/vector 3. Can anyone help me?

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

Answer by aldonaletto · Jan 07, 2012 at 11:33 PM

Get the empty object's position and add a random value to the x coordinate, then instantiate at this point:

var thePrefab : GameObject; var range: float = 10; // set the +/- range around the empty object

function Instantiate () { var pos = transform.position; // get this object's position pos.x += Random.Range(-range, range); // add a random x offset around it var instance : GameObject = Instantiate(thePrefab, pos, transform.rotation); }

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 Pixelen · Jan 08, 2012 at 12:12 AM 0
Share

This is exactly what I wanted, thank you so much! Extra kudos for the comments, they really helped me understand what each line of code was doing.

avatar image
1

Answer by Julien-Lynge · Jan 07, 2012 at 08:37 PM

You can move along the axes of the current transform with Transform.Translate and get the random component with Unity's Random function. As an example:

 var newTransform = transform.position;
 newTransform.Translate(Vector3.right * Random.Range(-10.0, 10.0));
 var instance : GameObject = Instantiate(thePrefab, newTransform, transform.rotation);


Comment
Add comment · Show 2 · 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 Pixelen · Jan 07, 2012 at 09:53 PM 0
Share

Hi, thanks so much for the help, if possible would you be able to edit the code in my first post and add in your code? I tried it and got loads of errors, being a noob at this :P Thank you!

avatar image Simon V · Jan 07, 2012 at 10:08 PM 0
Share

@Pixelen: We usually don't do that. It's easier for other people to have the answer in an answer and not in the OP. @NOAA_Julien: I'm not sure how it works in JS, but in C# you are better off creating a Random variable at the start and using it throughout the whole class or even the whole program. Reason for this is that it's based on time. The machine updates its time only every x ms, and updates can happen in that same timeframe. So you would end up with 2 or 3 prefabs instantiating in the same place if you created a new Random variable every time.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prevent changing position 1 Answer

How to rotate without a target vector 1 Answer

transform.right to vector3 1 Answer

Follow x rotation of a target object. 1 Answer

Phantom Axis Movement 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