Search Unity

Animation and Movement Combination

Discussion in 'Scripting' started by yazsh_, Oct 13, 2014.

  1. yazsh_

    yazsh_

    Joined:
    Oct 3, 2014
    Posts:
    10
    I have a groundCheck() function in my movement script that checks if the player is grounded so that it can know whether or not he can jump. In my animation script I am writing another groundCheck() function so it knows whether or not to call the jump animation.

    To halt myself from duplicating code I am considering setting up an event system for this function but that seems like a lot of work. Is it acceptable style to combine the two scripts, or is there a better organization method using interfaces that I can use to mitigate this problem?

    The duplicated function and its variables:

    public LayerMask whatIsGround;
    public CircleCollider2D groundCheck;

    public bool check(){
    Vector3 bottomOfCollider = newVector3(this.transform.position.x,this.transform.position.y - groundCheck.radius,0);

    returnPhysics2D.OverlapCircle(bottomOfCollider,.2f,whatIsGround);

    }
     
  2. sam268

    sam268

    Joined:
    Apr 21, 2014
    Posts:
    149
    I am assuming you are using a boolean in your jump script to tell if the character is grounded? If so, just get that boolean ausing getcomponent. For instance if ifGrounded = true, then the animation cannot be played. Or if isJumping = true, the animation can be played. You get the idea. But just remember you need to use getcomponent to get that boolean from the jump script
     
  3. yazsh_

    yazsh_

    Joined:
    Oct 3, 2014
    Posts:
    10
    Thanks!
     
  4. yazsh_

    yazsh_

    Joined:
    Oct 3, 2014
    Posts:
    10
    Actually, I have my movement script implimenting an interface for all its functions. Is it better style if I have the animation script use getcomponent(interface)?
     
  5. sam268

    sam268

    Joined:
    Apr 21, 2014
    Posts:
    149
    I normally prefer using getcompenent, however there is no truen"style" you should use. It is all just opinions. Whatever you find easier you should use!