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.
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…
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.