- Home /
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...
Answer by nhftk12 · Jun 07, 2011 at 01:28 PM
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;
You can achieve the same effect with cannon1.rigidbody.AddForce((transform.forward + transform.up) * 2000);
You didn't normalize the direction and just added the two vectors. If you're just guessing the force anyway this is propably of no concern, though you should know that the resulting force is not 2000 units. The solution of Bunny83 is the correct solution.
Answer by Tom 17 · Jun 07, 2011 at 11:53 AM
(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
Answer by Bunny83 · Jun 07, 2011 at 02:11 PM
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);
[...]
}
But not in a very effective way... the angle is just the result of two forces. I don't care if you solved it. This page is a collection of questions/answers for the community.
Answer by flaminghairball · Jun 07, 2011 at 01:35 PM
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.
Your answer
Follow this Question
Related Questions
Rotate object in a set amount of time 1 Answer
Rotate object in 45 degree steps 1 Answer
how to convert rotation of an object in degrees X 2 Answers
AddForce has no effect on my cannonball. 3 Answers
turn 180 spaceship 0 Answers