Ternary Operator?

The left/right hand seems like a textbook place for a ternary operator:

Instantiate(weaponPrefab,(isRightHanded?rightHandTransform:leftHandTransform));

1 Like

It sure is. It’s what I use in my own code, and I believe we do introduce the concept in a later course in the series.

For those reading this post who haven’t encountered Ternary Operators, they work like this

condition ? trueResult : falseResult

It’s syntactic sugar that the compiler interprets as

if(condition)
{
    return trueResult;
} else
{
    return falseResult;
}
2 Likes

I totally did, but I gave it some love with some spacing so it’s still readable :wink:

    public void Spawn(Transform rightHand, Transform leftHand, Animator animator)
    {
        if (null != equippedPrefab)
        {
            Transform handTransform = (isRightHanded) ? rightHand : leftHand;
            Instantiate(equippedPrefab, handTransform);
        }
        if (null != animatorOverride)
        {
            animator.runtimeAnimatorController = animatorOverride;
        }
    }

Handling of a null override controller might need some adjustment later on, when switching weapons gets expanded so the player can drop a weapon and revert to unarmed combat…

I’m thinking you might want to grab the current controller (which would be an override or you would have to check for if it is) and then set it back to what it originally was…

Privacy & Terms