Relative Bullet Trajectories

Good day, everyone!

I’m currently working on a zero-g space simulation, and I’ve run into a bit of a snag. Specifically in terms of firing projectiles via Instantiate. The issue that I’m running into is two-fold. a) It is possible to accelerate past the speed of the bullet, causing you to overtake bullets as they leave the barrel. Thus, kaboom. b) bullets fired to the side of the direction of travel snap back, as though caught in the wind that the craft is traveling through. As there is no wind in space, this is naturally an undesirable effect.

I’m hoping to find some way of making the bullets inherit the velocity of the object based on the direction of travel. IE, if you are travelling at a speed of 80 forward, and you turn to the side and start firing, the bullets will head straight out from your ship with that additional 80 forward speed. This would allow all projectiles to travel relative to the object that fires them. Unfortunately, I’ve yet to be able to find the attributes via code that would return this information. Any help would be appreciated.

I look forward to hearing from you! My current firing code is as follows:

var projectile : GameObject;
var parentO : GameObject;
var fireRate : float = 0.5;
var lifeTime : float = 0.5;
var accLoss : float = 0.0;

var weapSpeed : float = 0.0;
var numGuns : int = 2;
 var isLinked : boolean = false;
var gun1 : GameObject;
var gun2 : GameObject;
private var nextFire : float = 0.0;
private var activeGun : int = 0;
private var fireFrom : GameObject;

var shotSound : AudioClip;

private var tempX : float = 0.0;
private var tempY : float = 0.0;
function Update(){
	if(Input.GetKeyDown(KeyCode.I)){
		if(!isLinked){
			isLinked = true;
		}
		else{
			isLinked = false;
		}
	}
	
	if(Input.GetButton("Fire1")  Time.time > nextFire){
		if(!isLinked){
			nextFire = Time.time + fireRate;
			
			if(activeGun == 0){
				fireFrom = gun1;
				activeGun++;
			}
			else if(activeGun == 1){
					fireFrom = gun2;
					activeGun = 0;
			}
			tempX = fireFrom.transform.rotation.x+Random.Range(-accLoss, accLoss);
			tempY = fireFrom.transform.rotation.y+Random.Range(-accLoss, accLoss);
			clone = Instantiate(projectile, fireFrom.transform.position, fireFrom.transform.rotation);
			clone.rigidbody.velocity = parentO.rigidbody.transform.TransformDirection(Vector3.forward) + fireFrom.transform.TransformDirection(Vector3(tempX, tempY, weapSpeed));
			fireFrom.audio.PlayOneShot(shotSound);
		}
	
		else{
			tempX = fireFrom.transform.rotation.x+Random.Range(-accLoss, accLoss);
			tempY = fireFrom.transform.rotation.y+Random.Range(-accLoss, accLoss);
			tempX2 = fireFrom.transform.rotation.x+Random.Range(-accLoss, accLoss);
			tempY2 = fireFrom.transform.rotation.y+Random.Range(-accLoss, accLoss);
			nextFire = Time.time + (fireRate * numGuns);
			clone = Instantiate (projectile, gun1.transform.position, gun1.transform.rotation); //create the projectile at the selected gun position
			clone.rigidbody.velocity = gun1.transform.TransformDirection(Vector3(tempX, tempY, weapSpeed)); //Add velocity, including tempX and tempY for projectile spread.
			
			clone = Instantiate (projectile, gun2.transform.position, gun2.transform.rotation);
			clone.rigidbody.velocity = gun2.transform.TransformDirection(Vector3(tempX2, tempY2, weapSpeed));
			gun1.audio.PlayOneShot(shotSound);
			gun2.audio.PlayOneShot(shotSound);
		}
	}
}

If your characters are using physics, you should be able to add the characters rigidbody velocity to the bullet and get the behaviour you’re describing.

Your code is sort of messy and hard to follow, but as a guess, a change like this might work :

clone.rigidbody.velocity = parentO.rigidbody.velocity + parentO.rigidbody.transform.TransformDirection(Vector3.forward) + fireFrom.transform.TransformDirection(Vector3(tempX, tempY, weapSpeed));

Yeah, I forgot to comment it before posting. I will try the revised code as soon as I get home from work. I’m more for modeling and animation than scripting, but it’s been fun to learn so far.

Sycle, that code worked perfectly! Thanks! I’ll post a cleaned up version on this thread when everything is finished. I’m still learning how to organize all this coding stuff :slight_smile: Once that is done, I’ll comment it and set it up here for folks to use.

Again, thank you very much!