- Home /
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 :)
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;
 }
 
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,
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.
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);
 }
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!
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                