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.