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 AusAndrew19 · Aug 07, 2013 at 06:51 PM · rotationjavascriptinstantiatetransformforce

How to add random force/Rotation to bulletEject.

 var bulletshell = Instantiate(bulletPrefab,shellSpawn.position, shellSpawn.rotation);
         bulletshell.rigidbody.AddForce(transform.up + transform.right * 60);
         bulletPrefab.transform.rotation * Quaternion.Euler(Random.Range(-10,10),0,0);

is where i instantiate it. i tryed Random.rotation but that is really ugly.. But i can't figure out how to add random rotation/force so when the bullet is instantiated it looks more realistic, Does someone know how to make this work :)

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

1 Reply

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

Answer by clunk47 · Aug 07, 2013 at 07:01 PM

Add a ConstantForce component to your bullet shell prefab, then add a script to that prefab. In that script, set the torque of the constant force to be random like so:

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour
 {
     Vector3 torque;
     
     void Awake()
     {
         torque.x = Random.Range (-200, 200);
         torque.y = Random.Range (-200, 200);
         torque.z = Random.Range (-200, 200);
         constantForce.torque = torque;
     }
 }

Have a look here for more info on constantForce.torque.

EDIT: If you want to do this in JS instead of C#

 var torque : Vector3;
 
 function Awake()
 {
     torque.x = Random.Range (-200, 200);
     torque.y = Random.Range (-200, 200);
     torque.z = Random.Range (-200, 200);
     constantForce.torque = torque;
 }
 
Comment
Add comment · Show 6 · 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 AusAndrew19 · Aug 07, 2013 at 07:11 PM 2
Share

Your a genius, Plus you posted information on the subject, I really appreciate it :) Thank you! Seems it wont add random force on the Y axis, all just going out the side in different rotations,

avatar image clunk47 · Aug 07, 2013 at 07:17 PM 1
Share

For force ins$$anonymous$$d of torque, have a look at constantForce.force and constantForce.relativeForce. Relative will be on local axes, so be careful or they'll fly around crazily lol.

avatar image clunk47 · Aug 07, 2013 at 07:22 PM 1
Share

If you just want to make them fly up a bit, use rigidbody.AddForce on awake or start. Use Force$$anonymous$$ode.Impulse.

 var torque : Vector3;
 
 function Awake()
 {
     torque.x = Random.Range (-200, 200);
     torque.y = Random.Range (-200, 200);
     torque.z = Random.Range (-200, 200);
     constantForce.torque = torque;
     rigidbody.AddForce(Vector3.up * Random.Range(2, 5), Force$$anonymous$$ode.Impulse);
 }


avatar image clunk47 · Aug 07, 2013 at 07:23 PM 1
Share

Here's more info on Random.Range.

avatar image AusAndrew19 · Aug 07, 2013 at 07:44 PM 1
Share

Wow.. You are Awesome!! Thank you! I used. rigidbody.AddForce(Vector3.up * Random.Range(0.2, 1), Force$$anonymous$$ode.Impulse); adds a nice effect, Thank you for taking the time to explain and give me links to explain even further, Thank you!

Show more comments

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

16 People are following this question.

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

Related Questions

Trying to get a shoot function working in unity using instantiate and it isn't working. 1 Answer

Move an object toward an angle in 2d space 0 Answers

[JS] transform.rotation not working 1 Answer

Instantiate as a child at position 2 Answers

rb.addforce(localrotation?);,transform.addforce(localrotation?); 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