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

How to make enemy grab player with animation?

Discussion in 'Scripting' started by Haseeb_BSAA, Jan 16, 2015.

  1. Haseeb_BSAA

    Haseeb_BSAA

    Joined:
    Aug 20, 2014
    Posts:
    316
    Hi guys , I'm a beginner in C# and I can't figure out how to attach two game objects with each other using animations, just like a zombie grabbing the player with animation and we need to press buttons to break free.
    How is this possible? Can you please explain this with a simple script so I can understand how that basically works. Maybe you could explain by giving an example of two cubes , one cube grabbing other with animation.
    Any help will be fantastic! :)
     
  2. TheForsaken95

    TheForsaken95

    Joined:
    Aug 29, 2014
    Posts:
    30
    Well there is probably more than one way to do this, but I would align the zombie to the target position, and offset it a little bit so that they are not inside of each other.

    zombie.transform.position = (player.transform.position + new Vector3(10,0,0));

    Then you would probably want to be unable to move while the zombie was hitting you.

    (-Insert disable control script here.)

    From there you would need a script to determine how many times you have clicked before breaking free.

    int clickCount = 0;

    if(input.getMouseDown(0)) {
    clickCount += 1;
    }

    if(clickCount > 10) {
    doBreakFreeFunctionOfSomeSort();
    }

    At any point in your script you could include animations, if you are using Mecanim (The default unity animator) it would be quite easy, in fact the only thing you would need is a trigger to set off the animation.
     
    Haseeb_BSAA likes this.
  3. Haseeb_BSAA

    Haseeb_BSAA

    Joined:
    Aug 20, 2014
    Posts:
    316
    Thank you so much!! That really makes sense :) I'll try that out :)