- Home /
What are ways to give Scriptable objects a state?
In my game, I have different types of projectiles. I want these projectiles to have different trajectories, and I want these trajectories to be "pluggable".
For instance, I'd have a weapon monobehavior, and I would add a projectile prefab to it, and then a "trajectory" scriptable object to control the projectile path. Changing a projectile behavior would be as simple as setting a different trajectory in the inspector.
I have this working already:
// ProjectileTrajectory is a scriptable object
public class HeatSeekingTrajectory : ProjectileTrajectory
{
public override void Tick(Bullet bullet)
{
bullet.transform.rotation = bullet.transform.rotation.DiveInAtSpeed(
bullet.destination - bullet.transform.position,
bullet.rotationSpeed
);
bullet.transform.position += bullet.transform.forward * bullet.speed * Time.deltaTime;
}
}
That's just 1 example.
But some trajectories are more complicated than that, and they require changing direction at random, every x seconds for instance. So I need some state to know when to change direction again.
Where would you store this state? In the bullet monobehavior? Seems weird to me, because there are so many types of trajectories, the monobehavior shouldnt have to care about the different trajectories implementations...
Should I create new instances of ProjectileTrajectory
s, using the trajectory passed in the inspector as a template, and then storing the state within this particular instance? Is my best option to use Instanciate(scriptable_object)
, so that I have 1 instance per projectile? Does that cause overhead compared to just instantiating a regular class?
If you have any smart solutions, I'm all ears :))
Your answer
![](https://koobas.hobune.stream/wayback/20220612234910im_/https://answers.unity.com/themes/thub/images/avi.jpg)