Hey all,
I’m trying to make a turret fire at the player in a 2.5d game, but the turrets always fire “up” or towards the top of the screen. Let me give you the cliff notes on the mechanics:
Players and enemy’s use the same code for firing weapons and it functions like this:
- object loads an instance of the weapon on itself (Start Function) and passes the fire point (where the projectile will be instantiated) and rotation to the weapon script when the weapon is fired.
- The weapon instantiates the projectile with the correct rotation and position, then loads its stats (damage, velocity etc)
- The projectile moves in the given direction until it reaches the edge of the screen and is destroyed.
Object Hierarchy:
Object
-Turret
-Fire Point
I know there is an issue with my script (obviously) but I’m using myTransform.Translate (Vector3.forward * amtToMove); to push the projectile. Before I ask help with code, I’d like to know if this is logically correct or if I’m barking up the wrong tree entirely.
Thanks!
Post your script so we can find the errors. Remember to use code tags and mention in your title if its Js or C#. These tips will help get a better and faster response.
Well, I was hoping to find out if I was even using the right logic before getting into broken code. Teach a man to fish and all that. I’m not sure if I’m even tackling the problem the right way, or if I should be using transform.TransformDirection();. But here are some snippets:
Instantiate Weapon
if(equippedWeapons[0] != null){
while(i < equippedWeapons.Length){
GameObject weapon = Instantiate(equippedWeapons[i], transform.position, transform.rotation) as GameObject;
weapon.transform.parent = transform;
weaponBay[i] = weapon.GetComponent<WeaponStats>();
weaponHardpointVector[i] = transform.Find("EnemyTankTurret/EnemyWeapon1");
//weaponHardpointRotation[i] = transform.Find("EnemyTankTurret/EnemyWeapon1");
i++;
b++;
}
}
Firing Code
IEnumerator FireWeapons(){
// if(burstFire > Time.time){
// burstFire += (3 + Time.time);
int i = 0;
yield return new WaitForSeconds(1.5f);
while(i < equippedWeapons.Length){
Debug.Log("1 The barrel Rotation is: " + weaponHardpointVector[0].transform.rotation);
weaponBay[i].FireWeapon(weaponHardpointVector[i].transform.position,weaponHardpointVector[i].transform.rotation, false);
i++;
}
// }
instantiate projectile
public void FireWeapon(Vector3 firePoint, Quaternion fireRotation, bool isPlayer){
if(Time.time >= loadWeapon){
loadWeapon = Time.time + 60 / rateOfFire;
Quaternion weaponScatter = Quaternion.Euler(fireRotation.x , fireRotation.y + Random.Range (minCone + stabilityBonus, maxCone - stabilityBonus), fireRotation.z);
Debug.Log("Initial Pass Rotation: " + fireRotation);
//Fire Projectile
Projectile zProjectile = Instantiate (projectile, firePoint, weaponScatter) as Projectile;
Debug.Log("2 Projectile is being instatiated with rotation: " + weaponScatter);
//audio.PlayOneShot(soundFX);
//Pass weapon stats on to projectile
zProjectile.damage = Random.Range(weaponMinDamage,weaponMaxDamage + 1);
zProjectile.ion = Random.Range(weaponMinIon,weaponMaxIon + 1);
zProjectile.velocity = projectileVelocity;
zProjectile.attackType = attackType;
zProjectile.isPlayer = isPlayer;
}
}
Moving Projectile across screen
// Use this for initialization
void Start () {
myTransform = transform;
screenHeight = Camera.main.orthographicSize;
screenWidth = screenHeight * Camera.main.aspect;
Debug.Log("3 Projectile has been created with Rotation: " + myTransform.localRotation);
}
// Update is called once per frame
void Update () {
float amtToMove = velocity * Time.deltaTime;
myTransform.Translate (Vector3.forward * amtToMove, Space.Self);