• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Sugavanas

Jumping problem in Animator controller in unity3d

5 posts in this topic

confused.png Hello i saw the mecanim animation tutorial by unity and get to know abt it. So i made my own scene and made a controller with a new player using the same script from unity. I used new animation files from the 150 animation which unity gave for free.

Here is the script

 

 

c#

using UnityEngine;

using System.Collections;

 

// Require these components when using this script

[RequireComponent(typeof (Animator))]

[RequireComponent(typeof (CapsuleCollider))]

[RequireComponent(typeof (Rigidbody))]

public class BotControlScript : MonoBehaviour

{

    [System.NonSerialized]                  

    public float lookWeight;                    // the amount to transition when using head look

    

    [System.NonSerialized]

    public Transform enemy;                     // a transform to Lerp the camera to during head look

    

    public float animSpeed = 1.5f;              // a public setting for overall animator animation speed

    public float lookSmoother = 3f;             // a smoothing setting for camera motion

    public bool useCurves;                      // a setting for teaching purposes to show use of curves

 

    

    private Animator anim;                          // a reference to the animator on the character

    private AnimatorStateInfo currentBaseState;         // a reference to the current state of the animator, used for base layer

    private AnimatorStateInfo layer2CurrentState;   // a reference to the current state of the animator, used for layer 2

    private CapsuleCollider col;                    // a reference to the capsule collider of the character

    

 

    static int idleState = Animator.StringToHash("Base Layer.Idle");    

    static int locoState = Animator.StringToHash("Base Layer.Locomotion");          // these integers are references to our animator's states

    static int jumpState = Animator.StringToHash("Base Layer.Jump");                // and are used to check state for various actions to occur

    static int jumpDownState = Animator.StringToHash("Base Layer.JumpDown");        // within our FixedUpdate() function below

    static int fallState = Animator.StringToHash("Base Layer.Fall");

    static int rollState = Animator.StringToHash("Base Layer.Roll");

    static int waveState = Animator.StringToHash("Layer2.Wave");

    

 

    void Start ()

    {

        // initialising reference variables

        anim = GetComponent<Animator>();                      

        col = GetComponent<CapsuleCollider>();              

        enemy = GameObject.Find("Enemy").transform; 

        if(anim.layerCount ==2)

            anim.SetLayerWeight(1, 1);

    }

    

    

    void FixedUpdate ()

    {

        float h = Input.GetAxis("Horizontal");              // setup h variable as our horizontal input axis

        float v = Input.GetAxis("Vertical");                // setup v variables as our vertical input axis

        anim.SetFloat("Speed", v);                          // set our animator's float parameter 'Speed' equal to the vertical input axis              

        anim.SetFloat("Direction", h);                      // set our animator's float parameter 'Direction' equal to the horizontal input axis        

        anim.speed = animSpeed;                             // set the speed of our animator to the public variable 'animSpeed'

        anim.SetLookAtWeight(lookWeight);                   // set the Look At Weight - amount to use look at IK vs using the head's animation

        currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation

        

        if(anim.layerCount ==2)     

            layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1);   // set our layer2CurrentState variable to the current state of the second Layer (1) of animation

        

        

        // LOOK AT ENEMY

        

        // if we hold Alt..

        if(Input.GetButton("Fire2"))

        {

            // ...set a position to look at with the head, and use Lerp to smooth the look weight from animation to IK (see line 54)

            anim.SetLookAtPosition(enemy.position);

            lookWeight = Mathf.Lerp(lookWeight,1f,Time.deltaTime*lookSmoother);

        }

        // else, return to using animation for the head by lerping back to 0 for look at weight

        else

        {

            lookWeight = Mathf.Lerp(lookWeight,0f,Time.deltaTime*lookSmoother);

        }

        

        // STANDARD JUMPING

        

        // if we are currently in a state called Locomotion (see line 25), then allow Jump input (Space) to set the Jump bool parameter in the Animator to true

        if (currentBaseState.nameHash == locoState)

        {

            if(Input.GetButtonDown("Jump"))

            {

                anim.SetBool("Jump", true);

            }

        }

        

        // if we are in the jumping state... 

        else if(currentBaseState.nameHash == jumpState)

        {

            //  ..and not still in transition..

            if(!anim.IsInTransition(0))

            {

                if(useCurves)

                    // ..set the collider height to a float curve in the clip called ColliderHeight

                    col.height = anim.GetFloat("ColliderHeight");

                

                // reset the Jump bool so we can jump again, and so that the state does not loop 

                anim.SetBool("Jump", false);

            }

            

            // Raycast down from the center of the character.. 

            Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);

            RaycastHit hitInfo = new RaycastHit();

            

            if (Physics.Raycast(ray, out hitInfo))

            {

                // ..if distance to the ground is more than 1.75, use Match Target

                if (hitInfo.distance > 1.75f)

                {

                    

                    // MatchTarget allows us to take over animation and smoothly transition our character towards a location - the hit point from the ray.

                    // Here we're telling the Root of the character to only be influenced on the Y axis (MatchTargetWeightMask) and only occur between 0.35 and 0.5

                    // of the timeline of our animation clip

                    anim.MatchTarget(hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(0, 1, 0), 0), 0.35f, 0.5f);

                }

            }

        }

        

        

        // JUMP DOWN AND ROLL 

        

        // if we are jumping down, set our Collider's Y position to the float curve from the animation clip - 

        // this is a slight lowering so that the collider hits the floor as the character extends his legs

        else if (currentBaseState.nameHash == jumpDownState)

        {

            col.center = new Vector3(0, anim.GetFloat("ColliderY"), 0);

        }

        

        // if we are falling, set our Grounded boolean to true when our character's root 

        // position is less that 0.6, this allows us to transition from fall into roll and run

        // we then set the Collider's Height equal to the float curve from the animation clip

        else if (currentBaseState.nameHash == fallState)

        {

            col.height = anim.GetFloat("ColliderHeight");

        }

        

        // if we are in the roll state and not in transition, set Collider Height to the float curve from the animation clip 

        // this ensures we are in a short spherical capsule height during the roll, so we can smash through the lower

        // boxes, and then extends the collider as we come out of the roll

        // we also moderate the Y position of the collider using another of these curves on line 128

        else if (currentBaseState.nameHash == rollState)

        {

            if(!anim.IsInTransition(0))

            {

                if(useCurves)

                    col.height = anim.GetFloat("ColliderHeight");

                

                col.center = new Vector3(0, anim.GetFloat("ColliderY"), 0);

                

            }

        }

        // IDLE

        

        // check if we are at idle, if so, let us Wave!

        else if (currentBaseState.nameHash == idleState)

        {

            if(Input.GetButtonUp("Jump"))

            {

                anim.SetBool("Wave", true);

            }

        }

        // if we enter the waving state, reset the bool to let us wave again in future

        if(layer2CurrentState.nameHash == waveState)

        {

            anim.SetBool("Wave", false);

        }

    }

}

 

And all of them worked correctly with moving forward back. I arranged them made it settings and everything. But when i do for jump, it jumps correctly and continue to jump when i press up button. But when i replace it with my old jump file using the sme pace and position it jumps correctly. I know that i missed one setting between that two files. Below in the attachment i have added those two fbx files, please anyone see it and say what I did wrong, if you want more files including the controllers

0

Share this post


Link to post
Share on other sites

You are probably looping they jump animation (plays until it you let go of the button) while your old jump animation is set to not loop (so it only plays only once per button press). Check out this page for some help. As far as the script goes, it looks fine (then again, I have only a little experience using Unity3D).

0

Share this post


Link to post
Share on other sites

Hmmm... Does it only not work when you play the jumping animation? Can you test it on different animation?

 

As far as the script is concerned, try changing the if loop that checks for the jump button press to a while loop.

0

Share this post


Link to post
Share on other sites

I just tested it out and I don't see any looping (even when a run and jump into mid-air).

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0