I’m having an issue connecting a projectile to a turret in my game.
I am currently using a one-to-one relationship model between a projectile and the turret it came from. The turret send information (e.g. damage to enemies) to the projectile, and the projectile receives it. However, I’m having trouble figuring out how to get the projectile to actually receive the information from the turret.
The main problem lies within how the turrets behave in my game. Each turret has an upgrade system included which increases the amount of damage a projectile inflicts on a target. Each turret upgrades independently, however; given the one-to-one relationship mentioned earlier, this means that turret A could fire a projectile that inflicts more damage than turret B if A has upgraded more than B.
I currently have a script attached to the projectiles which I initially thought would receive information using a reference to a script attached to each turret in the game. However, the projectile’s scripts has no way of differentiating which turret it came from.
If anyone has any idea as to help, please let me know. Thank you for taking the time out to read this.
@naermataron, if you have main.cs code search for DebugMenu and disable it.
The easiest way would be to have the turret set the parameters of the projectile as it is launched. The code might look like:
var go = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
var projScript = go.GetComponent(ProjScript);
var projScript.damageFactor = damageFactor;
var projScript.maxSpeed = maxSpeed;
Where ‘damageFactor’ and ‘maxSpeed’ are changed/upgraded by the turret.
Another solutions is to have a unique tag associated with each turret that is assigned to the projectiles. Then you can use GameObject.FindGameObjectsWithTag() to get a list of all the projectiles fired by one turret.
And another solution to have a ‘fromTower’ variable on the projectile script that uniquely identifies what tower each projectile comes from. You would first get a list of all projectiles using FindGameObjectsWithTag(), then you could cycle through them and change the values of any with a specific tower tag.
And there are many other ways to go about your task. Note many games implement a object cache so they can reuse things like projectiles rather than use expensive Instantiate() and Destroy() calls. If you do this, then you can cycle through the entries in your cache.
I'm still having trouble internalizing your solution, so I've created a general scenario to help me out: each turret has a cache of n projectiles. Once one projectile from that cache is fired, it is given a tag "FiredProjectile." Projectile P, upon hitting its target, has to run a function which gives turret T, the turret P is fired from, experience points. This function, however, is located on a script attached to T that P is fired from. How do I get P to run a function connected to T?
@naermataron, if you have main.cs code search for DebugMenu and disable it.
– jun-yambao0312