Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fox-Handler · May 28, 2014 at 08:52 PM · mecanimjumproll

Jump Down and Roll

I took this script from the unity mecanim tutorial. I noticed that if I comment out everything except the last else if statement that it still works. From what I understand, it's originally written so that the center is lowered and upon impact curves the height and repositions the center of the collider so that the character does not appear to be floating. So why is it that if I comment all that out the last line still produces the same results?

Alright so it seems like for some reason they decided they wanted to change where the center of the collider was in the jumpdown state, but you only really need to use the curve for the height and center of the capsule collider on the rollstate. So it pretty much seems like the first lines of code aren't really doing anything. So if they are then I can't really tell.

Here is the entire script

 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); //lowering the COLLIDER (not the character inside!)...changing its center to be lower
     }
     
     // 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"); // changing the height/size of the collider
     }
     
     // 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"); // a curve set to change the height and then back to normal
             
             col.center = new Vector3(0, anim.GetFloat("ColliderY"), 0); // center of the collider needs to be reset...
             
         }
     }
     // 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);
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rockyourteeth · May 28, 2014 at 08:55 PM

Is this the whole script? If so, the reason it makes no difference is because you have "else if" statements without any "if" statements. "else if" only run if the "if" before it doesn't run. Those lines of code are not executing anyway, so commenting them out makes no difference.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fox-Handler · May 28, 2014 at 09:06 PM 0
Share

no this isn't the whole script. I guess i'm just wondering why it was included in the tutorial if the last line is all you need

avatar image rockyourteeth · May 28, 2014 at 11:02 PM 0
Share

It probably does something. Try posting the whole script (or at least more of it).

avatar image Fox-Handler · May 29, 2014 at 12:58 AM 0
Share

Posted the whole script. It's from the unity mecanim tutorial that uses the robot if you want to look at that too

avatar image rockyourteeth · May 29, 2014 at 06:47 PM 0
Share

Well, there's definitely a bug in the code on line 124 and below where there are "else if" statements without a beginning "if" statement. $$anonymous$$aybe line 124 should be an "if" ins$$anonymous$$d of an "else if"? Not sure.

avatar image Fox-Handler · May 30, 2014 at 02:51 AM 0
Share

Not sure. I can get pretty confused with if and else if statements if there are enough of them. Seems like they were just offsetting the center of the collider by referring to a curve in the first else if statment, and then just resetting it by referring to a different curve in the last one. But they could have just used a single curve.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mecanim RigidBody JUMP 0 Answers

Mecanim Inherit Velocity of previous animation? 0 Answers

How to make a character jump higher when using mecanim? 2 Answers

Mecanim tutorial issue with jump 2 Answers

mecanim boolean doesnt change -1 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges