- Home /
instantiating at different Angles?
I want to be able to instantiate a bullet object on an enemy tank with a specific angle trajectory. This is what I have so far...
function ShootCannon(){
//this is the position of fire at the muzzle of the tank
var firePos = Vector3(transform.position.x + 3.05,transform.position.y + 1.05,transform.position.z);
//this is the bullet creation
var instance = Instantiate(Bullet, firePos, transform.rotation);
instance.velocity = transform.TransformDirection (Vector3.forward * BulletSpeed);
//note: I want the rotation of the actually fireing to have an
//angle of +20 degrees from which the bullet comes out of.
Any help is appreciated! thanks!
}
Answer by maxkunes · Jan 03, 2014 at 04:26 AM
I see the way your doing it and I think that that is a unneeded approach to what you are trying to do. When ever I work with bullet spawns I would either use ray cast and there's a billion tutorial on that or the best option for what your doing is to make a bullet spawn object.
Create a empty game object Name it as Desired Position it linked to the tank's barrel as a child and rotate it to the desired degree Then fix the code...
var firePos : Transform;
function ShootCannon(){
//this is the bullet creation
var instance = Instantiate(Bullet, firePos.position, firePos.rotation);
instance.velocity = transform.TransformDirection (Vector3.forward * BulletSpeed);
//note: I want the rotation of the actually fireing to have an
//angle of +20 degrees from which the bullet comes out of.
In my opinion this is a very simple and effective way to solve your issue.
Warning Did not Test Code so there may be some simple errors but I dont think so...
I would make it like that but the reason I didnt go that route is because the tank is being spawned reused over and over again after the player destory's it. I could create an empty and make it a child of the tank at the end of the muzzle but are all the clone object scripts unique to the clone itself??
Never$$anonymous$$d lol you made me realize something. Im going to do it that way you said but ins$$anonymous$$d of having a separate script for the bullet spawner, im just going to use Transform.find();
and look up the transform and rotation of the bullet spawer form the tank AI Script. Thanks alot Playa!
This is the revised code to anyone needing it...
function ShootCannon(){
var BulletSpawnPoint: Transform;
BulletSpawnPoint = transform.Find("enemyTank/BulletSpawnPoint");
var instance = Instantiate(Bullet,BulletSpawnPoint.transform.position,BulletSpawnPoint.transform.rotation);
instance.velocity = BulletSpawnPoint.transform.TransformDirection (Vector3.forward * BulletSpeed);
}