Hello everybody! I’m creating my first serious game on Unity3D and I afraid about perfomance. Well move on to the issue:
I have controller script, which determines behaviour of created gameobjects (that are skills). So, as you know skills can have different logic, for example target skills have to move in target’s direction, buff skills have to follow a target etc. I don’t want classify all skills by their behaviour and create for each class own script. I decided I want to describe all logic (I mean transform logic) in one script.
So for example I have next code:
public bool followByTarget = false;
void FixedUpdate()
{
if (followByTarget)
{
transform.position = new Vector3(target.transform.position.x, startY, target.transform.position.z);
}
}
That means if i check followByTarget in edotor - skill effect will follow my target, otherwise it will not follow. If it is checked/unchecked on start it will be checked/unchecked the rest of the time. But every FixedUpdate method will make unnecessary conditional check. So I want to know is there any way to determine followByTarget value in Awake/Start method and use different Update functions in different cases. Or probably you can offer me another solution. Maybe it sounds stupid, but really any solution would be much better than writing code for each class of skill.
Enabled/Disable the script when you want to follow or not follow.
Disabled scripts don’t receive the Update/FixedUpdate call.
Another option is to remain always enabled (useful if you need to listen for other events like collision), but start/stop a coroutine whenever the followByTarget is toggled.
Thanks for reply! Sorry for no code tags, I updated my first post.
As you noticed I listen another events in script, moreover for some skills I need another actions in Update but don’t need follow a target (for example).
About your second option (start/stop coroutine) can you write example code please? Maybe I slow (there is night for me now) but I don’t see option to avoid conditional check with coroutine. I will try describe full picture:
[Tooltip("Determines direction for skill. If you dont need this leave [0,0,0] (by default).")]
public Vector3 direction = Vector3.zero;
public bool followByTarget = false;
public bool rotateByTarget = false;
void FixedUpdate()
{
// transforming position of skill if its need in direction vector.
if(direction != Vector3.zero)
{
transform.Translate(direction * speed * Time.deltaTime);
}
// make skill follow by target if its need
if (followByTarget)
{
transform.position = new Vector3(target.transform.position.x, startY, target.transform.position.z);
}
// make skill rotate by target if its need
if (rotateByTarget)
{
transform.rotation = target.transform.rotation;
}
}
As you can see i have here 3 variables (direction, followByTarget, rotateByTarget). So for example in situation when i set direction as default value (zero vector), followByTarget as true, rotateByTarget as true I want to avoid first conditional check (is direction equals Vector3.zero). Sure it would be cool to avoid another conditional checks too.