[2D] Set the instantiated projectile rotation from turret using generic list? There are multiple turret prefabs in the scene
Background Info
I have 2 different turret prefabs for placement on the left & right walls of the level. I'm using an animation to make the barrel auto-rotate up and down. When it sees the player through line-cast, it shoots at the player.
For the player, I used GameObject.FindGameObjectWithTag() to get the 2 vector points needed to calculate the correct angle of the projectile. Since there are multiple turret prefab instances in the scene, this isn't going to work.
Code Sample for calculating the angle of player projectile
// Angle & Direction of projectile
public GameObject aim_target;
private Vector3 aim_pos; // Vector of aim_target position
private Vector3 shot_pos; // vector of shotPoint position
private float ShootAngle; // angle for z-axis
public Vector3 rotateAim; // vector3 to set rotation angle from ShootAngle
// Spawn Point for projectile
public GameObject shotPoint;
void Awake()
{
shotPoint = GameObject.FindGameObjectWithTag ("spawnpoint");
aim_target = GameObject.FindGameObjectWithTag ("aim");
}
void Start()
{
aim_pos = aim_target.transform.position;
shot_pos = shotPoint.transform.position;
aim_pos.x = aim_pos.x - shot_pos.x;
aim_pos.y = aim_pos.y - shot_pos.y;
ShootAngle = Mathf.Atan2 (aim_pos.y, aim_pos.x) * Mathf.Rad2Deg - 0;
// Set vector for bullet angle
rotateAim = new Vector3 (0, 0, ShootAngle);
// set rotation of bullet
transform.rotation = Quaternion.Euler(rotateAim);
}
My problem is setting the correct rotation for the turret projectile after it has instantiated. I know that generic lists would be good to use for my purposes, but I don't know how to properly apply it.
I need to help on how to do the following:
Create generic list of all turrets in the level
Get the InstanceID for the turret that instantiated that specific projectile.
Use that gameobject reference to execute the code that will calculate and set the proper rotation of the projectile on z axis
Alternatively, I thought about trying to directly apply the turret's rotation to the projectile. I still don't know how to do that correctly in this situation.
Answer by Xanatos_88 · May 06, 2016 at 01:18 AM
I fixed the issue with the rotation!! I didn't use a generic list though. I placed the code that adjusted the projectile's rotation into the ShootPlayer() function of Turret AI script. The turret AI script is located on the turret barrel object, which is a child of the turret base parent object.
Shoot Player Function
void ShootPlayer()
{
if (targetFound && Time.time > nextFire && !shotDone)
{
// Time until next shot
nextFire = Time.time + fireRate;
int ammoIndex = 0;
float shootAngle;
enemyBullet = (GameObject)Instantiate (ammo [ammoIndex], turretShotPoint.transform.position, Quaternion.identity);
Vector2 direction = player.transform.position - transform.position;
direction.Normalize ();
sight_pos = sightEnd.position;
shot_pos = turretShotPoint.transform.position;
sight_pos.x = sight_pos.x - shot_pos.x;
sight_pos.y = sight_pos.y - shot_pos.y;
shootAngle = Mathf.Atan2 (sight_pos.y, sight_pos.x) * Mathf.Rad2Deg - 0;
// Set vector for bullet angle
rotateLeft = new Vector3 (0, 0, shootAngle);
// set rotation of bullet
enemyBullet.transform.rotation = Quaternion.Euler(rotateLeft);
if (facingRight) {
enemyBullet.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
} else if (!facingRight) {
// enemyBullet.rigidbody2D.velocity = new Vector3(Mathf.Cos(shootAngle*Mathf.Deg2Rad),Mathf.Sin(shootAngle*Mathf.Deg2Rad),0)* bulletSpeed;
enemyBullet.transform.LookAt (sightEnd);
enemyBullet.transform.rotation = Quaternion.Euler(rotateLeft);
enemyBullet.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
}
}
shotDone =!shotDone;
}