- Home /
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...
normalised Reference velocity * bullet speed = final bullet velocity
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);
}
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
Follow this Question
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