Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to apply a movement animation while maintaining scripted movement? - *SOLVED*

Discussion in 'Animation' started by Jacksonion, Aug 23, 2022.

  1. Jacksonion

    Jacksonion

    Joined:
    Nov 19, 2021
    Posts:
    7
    Disclaimer, I'm very new to all of this, so thank you in advance for any help!

    In my game, ghost-like enemies spawn in and float across the screen. Their movement is controlled via script. I'd like to apply a bobbing motion where they slowly float up and down as they glide across the screen.

    When I tried to use an animation to achieve this, I was able to create a pretty good bobbing animation by changing the transform position.y to be slightly higher/lower and then looped it. However, doing it this way prevented the enemy from moving across the screen. It was stuck in the position that was set for the x, y, and z values, as were all new enemies that spawned in.

    I'm clearly missing something here. I need a way to keep the motion set in the script while applying a bobbing animation/motion on top of it.
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    Animations always have priority over other stuff like scripts. If you animate an object's position, nothing else will be able to affect that property. You may also notice that if you move your ghost enemy to a different position, it will snap back to where you originally animated it - this is because you're animating its world position.

    The way around this is to attach your ghost to a parent object and move the parent object with your script, not the ghost. Your parent object won't be animated, so the script can move it around freely while the animated child ghost inherits its position.

    If anything is unclear, please let me know.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,172
    If you only need the one curve for y-offset, it's probably easier to expose an
    public AnimationCurve
    in your movement script and then use that to get the y-position.
     
    Last edited: Aug 30, 2022
  4. Jacksonion

    Jacksonion

    Joined:
    Nov 19, 2021
    Posts:
    7
    Thank you both for your replies! I'm attempting the scripting method now. Trying to figure out how to format the ICode part, or where I can grab that from. I joined the Discord linked in your profile as well @Baste :D
     
  5. Jacksonion

    Jacksonion

    Joined:
    Nov 19, 2021
    Posts:
    7
    Thanks again for this, I'm pretty much lost when it comes to adding the code for this. Do you know if there's a way to grab the code somehow from the existing animation curve that I set up and have it looped? Just having some trouble with the formatting. Thanks again for the response.
     
  6. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Save yourself some trouble man: https://drive.google.com/file/d/184JCZf_E14wD6YkVAd1HYuK_DyNKUfPu/view?usp=sharing

    The link is a 46 second video example on how to set up your objects to accomplish what you want.

    There's nothing wrong with doing this if you want, but it's not "easier." Dropping your movement script on a parent object takes literal seconds to do and requires no additional code.
     
    Last edited: Aug 30, 2022
  7. Jacksonion

    Jacksonion

    Joined:
    Nov 19, 2021
    Posts:
    7
    @Unrighteouss Thanks for the video! That explained it perfectly and I was able to apply that solution. I wasn't able to get the other method to work, I was probably missing something still. Thanks for the help everyone.
     
    Unrighteouss likes this.