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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by carter-carl30 · May 30, 2012 at 09:59 PM · spawnaxistransformdirection

random direction just in 2d

I have this script to spawn a powered up ball from a brick when hit. What I want it to do is spawn and travel in a random direction. BUT only in the X and Y axis (eg upwards, up-right, right, down-right, down, down-left...etc...) I don't know the wording to add in: instancesuperbullet.rigidbody.AddForce(transform.down * shootforce);

Would it be ...(transform.random, x, y); something like that?

thanks

 var prefabsuperbullet:Transform;
 var shootforce:float;
 
 function OnCollisionEnter ( collision : Collision) {
 
   if (collision.collider.name == "Bullet"){
         var instancesuperbullet = Instantiate(prefabsuperbullet, transform.position, Quaternion. identity); 
         instancesuperbullet.rigidbody.AddForce(transform.down * shootforce);
     }
 }
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
1

Answer by rutter · May 30, 2012 at 10:15 PM

Not sure exactly what you mean by "only in the X and Y axis", but I'll make an educated guess.

You could try something like this:

 var x = Random.Range(-1f, 1f);
 var y = Random.Range(-1f, 1f);
 var direction = Vector3(x, y, 0f);

 //if you need the vector to have a specific length:
 direction = direction.normalized * desiredLength;
Comment
Add comment · Show 7 · 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 carter-carl30 · May 30, 2012 at 10:23 PM 0
Share

to clarify, i'm making a breakout game 2d, and i have a brick that when you hit it, it spawns another ball. So I don't want it to spawn on the z axis or it would miss the bricks. Does this make any sense?

avatar image rutter · May 31, 2012 at 01:04 AM 0
Share

Sounds like my guess was more or less on the mark: a vector with randomized X and Y components, but a constant (probably zero) Z component.

avatar image carter-carl30 · May 31, 2012 at 07:47 PM 0
Share

Is this the correct way to add the information you gave me into my script? :-

var prefabsuperbullet:Transform; var shootforce:float; var x = Random.Range(-1f, 1f); var y = Random.Range(-1f, 1f); var direction = Vector3(x, y, 0f);

function OnCollisionEnter ( collision : Collision) {

if (collision.collider.name == "Bullet"){ var instancesuperbullet = Instantiate(prefabsuperbullet, transform.position, Quaternion. identity); instancesuperbullet.rigidbody.AddForce(transform.direction Vector3(x, y, 0f) * shootforce); } }

avatar image carter-carl30 · Jun 05, 2012 at 12:44 AM 0
Share

@rutter is this correct ?

avatar image carter-carl30 · Jun 20, 2012 at 03:53 PM 0
Share

Hi guys still about?

Show more comments
avatar image
0

Answer by jtbentley · Jun 05, 2012 at 02:25 AM

var instancesuperbullet = Instantiate(prefabsuperbullet, Vector3( Random.Range(0, 2), Random.Range(0, 2), 0), Quaternion. identity);

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 carter-carl30 · Jun 06, 2012 at 06:21 PM 0
Share

@JTBentley hi mate, I tried what you suggested in my script but when I hit my brick with my ball it spawned the superbullet from the centre of the screen in an upwards direction each time. Did I add it incorrectly?

var prefabsuperbullet:Transform; var shootforce:float;

function OnCollisionEnter ( collision : Collision) {

 if (collision.collider.name == "Bullet"){

var instancesuperbullet = Instantiate(prefabsuperbullet, Vector3( Random.Range(0, 2), Random.Range(0, 2), 0), Quaternion. identity);

} }

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

6 People are following this question.

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

Related Questions

transform.position.x ? 1 Answer

Can't get object to move and rotate on its own axis 2 Answers

How do i lock out a axis 1 Answer

Inverse the mouse position value 2 Answers

Lock a rotation 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