Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Need help with animating

Discussion in 'Animation' started by DordusRising, Mar 27, 2023.

  1. DordusRising

    DordusRising

    Joined:
    Dec 13, 2022
    Posts:
    37
    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.
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    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.

    1. 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.

    2. 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.

    3. 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:
    Code (CSharp):
    1.  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.

    Code (CSharp):
    1. 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.
    https://docs.unity3d.com/ScriptReference/Animator.SetTrigger.html
     
    DordusRising likes this.
  3. DordusRising

    DordusRising

    Joined:
    Dec 13, 2022
    Posts:
    37
    Thank you for helping me out! I'm trying to recreate Metroid Prime mechanics and I want the Arm Cannon to animate.
     
  4. DordusRising

    DordusRising

    Joined:
    Dec 13, 2022
    Posts:
    37
    Thanks for your solution! I will try it when I get the chance!
     
  5. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @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%.
     
  6. DordusRising

    DordusRising

    Joined:
    Dec 13, 2022
    Posts:
    37
    As much as I agree with you, you don't need to be rude.
     
  7. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @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.
     
  8. DordusRising

    DordusRising

    Joined:
    Dec 13, 2022
    Posts:
    37
    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.