1

I'm creating a code that will trigger an animation float when collided, but I want to code it to where it also turns off the float/animation when hit by the same trigger;

var TiltingL = TiltL ? player.camera.camAnim.SetBool("TiltL", true) : player.camera.camAnim.SetBool("TiltL", false);

I want to use the "? :" code, but is unsure how to properly code it with the animation.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Basically the above code is an if statement like this: if(TiltL) {TiltingL = player.camera.camAnim.SetBool("Tiltl",true);} else {TiltingL = player.camera.camAnim.SetBool("TiltL", false);} Basically you don't set any trigger. You just assign the "TiltingL" – Pavlos Mavris Mar 15 '23 at 08:09
  • Please do not spam tags: `visual-studio`, `animation` and `unityscript` are irrelevant in this question. – Max Play Mar 15 '23 at 08:38

2 Answers2

1

The ternary operator (?:) isn't a good fit for this use case. Instead, you could do:

// Get current value of animator bool
bool isFloating = player.camera.camAnim.GetBool("TiltL");

// Toggle animator boolean value
player.camera.camAnim.SetBool("TiltL", !isFloating)

The ternary operator is meant for returning one of two possible values, not for executing one of two possible methods (unless those methods both have the same return type as the variable you're assigning; see this answer for more details). Unity's animator.SetBool returns void, so your example would init TiltingL to void and cause an error.

  • You *could* stick to `TiltingL` but then rather do `TiltingL = !TiltingL; player.camera.camAnim.SetBool("TiltL", TiltingL);` but yeah you would need to ensure you always keep that in sync => I would use a property – derHugo Mar 15 '23 at 08:16
1

Is this as of your requirements?

Player.camera.camanim.setbook ("tiltl", player.camera.camanim.getbook ("tiltl")? False: TRUE);

The previous ANSWER was deleted. If this helps you, you can mark it so that it can help more people.

sssr
  • 364
  • 1
  • 6