I have an Arm Cannon, modeled after Samus’s Arm Cannon, that needs a some animation in order to spice it up. I need a recoil animation that triggers when it fires the Power Beam. I have tried using scripts to animate, but my C# scripting knowledge is very limited since I am a n00b at both C# and Unity’s animation system. I know how to create animations, but getting them to trigger once when a button is pressed is harder for me.
Hi,
If coding is not your strongest skill at the moment, I would suggest you could look into using Animator.
Here’s a rough outline of what I would do.
Create an animation that performs the recoil movement. You might also need to consider layering the animation, since it might be that your character needs to be able to shoot while standing idle, jumping, sliding, falling and so on. But that is another topic in itself.
Use Mecanim (Animator) to set up the animation, create an animation state where this effect happens. Then create a transition to this state, which is triggered. Seach for this information, there’s plenty of guides available how to build animation state machines in Animator. And there’s also the manual.
Now create a simple code which will trigger the animation in your game logic.
First create a reference to your Animator, and expose it in the Inspector. Drag the Animator component here.
i.e. something like this:
Animator animator;
Then, somewhere in your logic where this state should be called, call the specific Animator state and trigger it. When the Animator is correctly configured, your animation will transition to this state, the recoil animation will play and after it has played, the Animator will return to the state it was in.
animator.SetTrigger("Shoot");
In this imaginary example the trigger would be called Shoot in the Animator.
Anyway just look at the manual for more complete code example.
@susainjames Hi and welcome,
Sorry if I’m wrong but your answer sounds much like a Chat GPT answer. If you went through that effort of writing a real answer, you could have had some common sense to edit your post to have a code block for the example code. And with a Chat GPT detection tool your text gives a score close to 50%.
@DordusRising I am not being rude. Yes the answer gives probably the information you need. But think about the near future, what will happen when people start spamming here GPT answers, or oven worse, make bots that post automatically here? It will quickly pollute the forums with bot generated content. And while I don’t want to speak for others, I don’t personally want to see such situation. So I feel it’s a valid concern/doubt which I can voice here.
I understand your concern, but “you could have had some common sense” is mean to say. It would have been better to say “you would have likely” instead. Personally, while I think it is better to learn and gain the knowledge, I don’t see bot-generated code as an issue. Polluting the forums is an issue that I understand, however.