c# - Press a in game Button to play a animation -


i thought adding ingame button game. it's not gui or ui button, it's block, added wall example.

dummy code:

ontriggerenter(c:collider)  {      if(c.gameobject.tag =="player")      {        //text = "e interact!"         if(key.pressed("e")       {          //connect button specific block,      play animation       }      }    } 

so how connect specific block button, , if press e, play animation on specific block? please keep in mind i'm new in unity. helping out!

i don't know if managed create animation, i-ll explain scratch. when in editor, select door , press ctrl+6 open animation window. here can animate block. when done creating animation, block object have new script attached it: animator. can see animator state machine in animator window

these 2 different things:

  • animation: defines single animation (translation, rotation, change of color, ...)

  • animator: defines when animation occurs corresponding gameobject. animator can have variables (for example, bool) define next animation played

any object can have animator (your button can have 1 move when pressed. door can have 1 open / close)

for instance, in button animator, should have 3 states: idle, press, unpress. state press contain animation "press" speed 1. state unpress contain animation "press speed -1

then, still in animator window, create links between idle , 2 other states , add trigger condition called "onpress" (for example)

you can same animate door

in button code, write

public animator door; // in editor, give reference door. must have animator script work  ontriggerenter(c:collider)  {      if(c.gameobject.tag =="player")      {         //text = "e interact!"           if(key.pressed("e")         {            getcomponent<animator>().settrigger("onpress"); // button's animator goes "pressed" state            door.settrigger("open");  // door's animator goes "open" state         }      }   } 

then add trigger unpress button

one more thing: when "connect button block", feel misunderstood something: button script should added block in editor

look @ these 2 links more information on animations:

http://docs.unity3d.com/manual/animeditor-usinganimationeditor.html http://docs.unity3d.com/manual/animatorwindow.html


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -