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 Williamson · Sep 01, 2014 at 11:29 AM · physicsvariablesbullet

How do I create a bullet whose velocity and direction is affected by the velocity and direction of its source?

The idea that I have in mind is to create a variable that is equal to the direction and velocity of the gameobject (WarBird) that WarBirdFly.js (second script shown) is connected to, then add that variable to the "rigidbody.AddRelativeForce(Vector3.forward*BulletSpeed)" statement in BulletScript.js (below).

Here is the script (BulletScript.js) that goes on my bullet prefab:

 var BulletSpeed = 100;
   
 function Update (){

 rigidbody.AddRelativeForce(Vector3.forward*BulletSpeed);
 
 Destroy();
 }
 
 function Destroy(){
 yield WaitForSeconds (5);
 Destroy (this.gameObject);
 }

And here is the script (WarbirdFly) for the vehicle whose velocity and direction I want to affect the bullet:

 var forwardSpeed = 5.0;
 var turnSpeed : float = 2;
 var boostSpeed = 5.0;
 private var boostDirection = 0;
 private var boost = 0;
     
 function Update () {
 
     var forward = Input.GetAxis("SubspaceForward")+(boost*boostDirection);
     var turnAmount = Input.GetAxis("SubspaceYaw")*turnSpeed;
     var shipDirectionVelocity = rigidbody.velocity.magnitude;
     
     if(Input.GetButtonDown("SubspaceThrust")) {
         boost = boostSpeed;
         }
     if(Input.GetButtonUp("SubspaceThrust")) {
         boost = 0;
         }
     
     if(Input.GetAxis("SubspaceForward") > 0) {
         boostDirection = boost*1;
         }
     if(Input.GetAxis("SubspaceForward") < 0) {
         boostDirection = boost*-1;
         }
     if(Input.GetAxis("SubspaceForward") == 0) {
         boostDirection = 0;
         }
             
     rigidbody.AddRelativeForce((Vector3.right*forwardSpeed*forward));
     transform.Rotate(0,0,turnAmount); 
     rigidbody.angularVelocity = Vector3.zero;
             
 }

I've been trying to call the variable using this on BulletScript.js:

 var BulletSpeed = 100;
 var sscontrolInstance : warbirdFly;
 sscontrolInstance = GameObject.Find("Warbird").GetComponent(warbirdFly);
 var targetObj : Transform;

  function Update (){
 
     rigidbody.AddRelativeForce(Vector3.forward*BulletSpeed + sscontrolInstance.shipDirectionVelocity);

I know my var shipDirectionVelocity is dead wrong - I've made it lots of other things.

I basically want to make my rigidbody.AddRelativeForce statement (the one on my ship's gun) into a variable and I want to add that variable to the rigidbody.AddRelativeForce statement on my bullet, right?

By the way, these are relatively slow moving bullets, so rigidbodies should be fine...

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 supericecream · Sep 01, 2014 at 12:11 PM 0
Share

normalised Reference velocity * bullet speed = final bullet velocity

1 Reply

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

Answer by kariwmklawm · Sep 01, 2014 at 12:49 PM

From what I can understand, you want to fire a bullet that initially inherints the rotation and velocity of its parent before firing off forward relative to the shooting objects rotation? For illustration I made the bullets instantiated, you could simply set the bullets rotation/position without making a new one.

All you need is AddRelativeForce Vector3.forward, the rotation from the shooter and a direct copy of the shooting objects velocity as the bullet is getting shot. Put it straight into the bullets velocity and it will follow that initial velocity it was fired at. I added two to the Y axis of the spawned bullet otherwise it collides when spawned with the shooting script (I was using cubes).

Example Shooter Script:

 #pragma strict
 
 var Bullet : GameObject;
             
 function Update ()
 {
     if (Input.GetKeyDown("space"))
     {
         var bullet : GameObject =
         Instantiate(Bullet, transform.position 
         + (Vector3.up*2), transform.rotation);
         bullet.rigidbody.velocity = rigidbody.velocity;
     }
 }

Which will fire the bullet with this script:

 var BulletSpeed = 100;
  
 function Update ()
 {
     rigidbody.AddRelativeForce(Vector3.forward*BulletSpeed);
 }
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 Williamson · Sep 01, 2014 at 07:49 PM 0
Share

Thank you! I was certainly over thinking things. I didn't post my shooter script, which was working just fine...I just didn't think to calculate everything there, for some reason I wanted to do it on the bulletScript.

I am using a bulletSpawn, so no need for Vector3*2, but having the bullet.rigidbody.velocity = rigidbody.velocity in the script did the trick. Just for clarity's sake for passers-by:

 var bulletSpawn : GameObject;
 var Bullet : GameObject;
 var Ammo = 1;
 var Shoot = true;
 var animator : Animator;
 
 function Update () {
 
     if(Ammo ==0) {
         Shoot = false;
         Reload () ;
         Ammo = 1;
     }
     
     if(Shoot == true) {
         if(Input.GetButtonDown("Fire1")) {
             animator.SetBool("Shoot",true);
             var bullet : GameObject = 
             Instantiate(Bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
             bullet.rigidbody.velocity = rigidbody.velocity;
             Ammo--;
     }

 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Bullet Management: Possible Without Rigidbody? 3 Answers

Machine gun bullets - Raycast or Collider? 2 Answers

How do I make certain parts of a mesh hide in runtime 1 Answer

bullet doesn't go anywhere 1 Answer

How do I break apart an object? 2 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