shoot cannon ball in 45 degrees

I made a tank with 3 weapons but I want to ask about the second: the cannons, and be ready for more questions about it if there will (it not right to ask something I didn’t start yet…). On my tank there is 2 cannons that placed in 45 degrees on the rear.

This is the tank’s cannon shooting function:

function ShootCannons ()
 {
	 if (canShootCannon)
	 {
		 var cannon1 = Instantiate(cannonBall, gameObject.Find("LCSpawnPoint").transform.position, transform.rotation);
	     cannon1.gameObject.tag = "CharacterProjectile";
	     cannon1.rigidbody.AddForce(transform.forward * 20000);
	     yield WaitForSeconds (0.1);
		 var cannon2 = Instantiate(cannonBall, gameObject.Find("RCSpawnPoint").transform.position, transform.rotation);
	     cannon2.gameObject.tag = "CharacterProjectile";
	     cannon2.rigidbody.AddForce(transform.forward * 20000);
	     canShootCannon = false;
		 yield WaitForSeconds (7.0);
		 canShootCannon = true;
	 }
 }

The cannons shoot the cannon balls always forward, it seems the cannon balls just spawn from the cannons and go not in 45 degrees like in should but in 0 degrees (forward). How to make the tank shoot in 45 degrees so it will seems I shoot from the cannons? I think I know where the problem is but I not sure how to fix this.

Sorry for poor english and if this is a bad question, I didn’t find nothing about shooting in degrees…

(Quaternion.AngleAxis(45,transform.right) * transform.forward) * 20000

or

(Quaternion.AngleAxis(-45,transform.right) * transform.forward) * 20000

try those

EDIT: rearranged terms after testing with compiler, sorry for any inconvenience

I’m a bit confused, to which GameObject is that script attached? If it’s the tank, you shouldn’t use the tank’s transform for the shooting. I see you use a spawnpoint GO, if you rotate the apawnpoint so that it’s z axis (forward) points into the right direction you can use this transform.forward. That would be the “usual” way.

var leftCannon : Transform;
var rightCannon : Transform;

function Start()
{
    leftCannon  = gameObject.Find("LCSpawnPoint").transform;
    rightCannon = gameObject.Find("RCSpawnPoint").transform;
}

function ShootCannons ()
{
    [...]
    var projectile1 = Instantiate(cannonBall, leftCannon.position, leftCannon.rotation);
    projectile1.rigidbody.AddForce(leftCannon.forward * 2000);
    
    // or since the projectile is rotated the same way:
    projectile1.rigidbody.AddForce(projectile1.transform.forward * 2000);
    [...]
}

What happened is that you are shooting off the cannon ball in the tank’s forward direction, which would most likely not be a 45 degree rotation.

Another note: You will probably want to cache a reference to the SpawnPoint transforms instead of using gameObject.Find(). GameObject.Find() is a search function, and takes more time than just using a reference, i.e:

var lcSpawnPoint : Transform; //assign this in the inspector
var rcSpawnPoint : Transform; //assign this in the inspector

And then just drop in those references instead of the Find() call.

Never mind, I found out alone:

var cannon1 = Instantiate(cannonBall, gameObject.Find("LCSpawnPoint").transform.position, transform.rotation);
	     cannon1.gameObject.tag = "CharacterProjectile";
	     cannon1.rigidbody.AddForce(transform.forward * 2000);
		 cannon1.rigidbody.AddForce(transform.up * 2000);
	     yield WaitForSeconds (0.1);
		 var cannon2 = Instantiate(cannonBall, gameObject.Find("RCSpawnPoint").transform.position, transform.rotation);
	     cannon2.gameObject.tag = "CharacterProjectile";
	     cannon2.rigidbody.AddForce(transform.forward * 2000);
		 cannon2.rigidbody.AddForce(transform.up * 2000);
	     canShootCannon = false;
		 yield WaitForSeconds (7.0);
		 canShootCannon = true;