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 coffiarts · Feb 26, 2013 at 01:24 PM · animationcollideranimatorcurves

Can I use Animator.bodyPosition for modifying the colliders during an animation?

Hi everybody,

I want to manipulate my player object's collider in realtime during a jump animation.

I've read that this should best be done by using curves, but I am unsure whether this is possible in my case: I am using a blender created model, imported as generic rig. My animations don't seem to support curves (as I can't see a "Curves" item in the animations' properties).

That's why I am trying to maipulate my collider position and height by custom scripting within the Update() function. But I don't know which frame-by-frame values I could use to apply size changes to my collider.

By using the Monodevelop debugger, I've found out by chance that my animator has a property at runtime called Animator.bodyPosition.normalized, which returns normalized Vector3 objects like "(1.0, 1.1, 0.9)". These values seem to change from frame to frame during jumps, so I guess that they reflect the relative position of my model - which would be exactly what I am looking for!

However, trying to apply these values to my collider's position produces strange results (such as making the collider shoot into the air, fall through the ground, or just pop up about 1 unit and then staying there).

This is what I am doing in code, roughly:

 private var capsCollider:CapsuleCollider;
 private var animStateBase:AnimatorStateInfo;
 private var stateJump = Animator.StringToHash("BaseLayer.Jump");
 
 function Start() 
 {
     capsCollider = collider as CapsuleCollider;
     anim = GetComponent(Animator);
 }
 
 function Update() 
 {
     animStateBase = anim.GetCurrentAnimatorStateInfo(0); // 0 = BaseLayer
     
     [...]
     
     if (animStateBase.nameHash == stateJump)
     {
         // note that capsColliderYBase has been initialized before when jump animation started with the collider's original Y position
         capsCollider.center.y = capsColliderYBase * anim.bodyPosition.normalized.y;
         [...]
     }
     [...]
 }

I wonder: Ist it generally ok what I am trying to do, or is it rather crab to rely on anim.bodyPosition.normalized? Or am I doing something wrong apart from that in my code?

I'd be glad if someone could have a quick look at this!

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 coffiarts · Mar 07, 2013 at 09:50 AM

Once more an update to my own question. I assume that this can be considered a final answer:

I was completely unaware that there's a much easier way for achieving what I am trying to to:

One can simply use AnimationCurve for defining a custom curve as a script variable, which can then be freely defined in the curve editor. Inside my script, one can then use the current animation's normalizedTime to grab a specific value from the curve and apply it to the collider.

My current solution is still a bit quick & dirty, but it basically works:

 #pragma strict
 /* This script demonstrates (quick & dirty)
    How a collider's Y position can be dynamically adjusted
    during a running jump animation
 */
 
 // the following variable will be visible inside the Inspector window as a curve.
 // Simply click it to open the curve editor for freely adjusting it to your needs
 // Be aware that in the approach suggested here, 
 // - the curve's X axis values must always cover the range from 0 to 1 (exactly), 
 //   because we're using the animation's normalized time (0-1) as X values
 // - the curve's Y axis values are interpreted as multiplication factors for the 
 //   original Y position (i.e. they should have reasonable max values like 1.5)
 var colliderYCurve:AnimationCurve = AnimationCurve(Keyframe(0, 1), Keyframe(0.5, 1.5), Keyframe(1, 1));
 
 private var animStateBase:AnimatorStateInfo;
 private var stateJump:int = Animator.StringToHash("BaseLayer.Jump");
 private var capsCollider:CapsuleCollider;
 private var capsColliderYBase:float;
 
 
 function Start()
 {
     // initialize the collider and store its initial Y position
     capsCollider = GetComponent(CapsuleCollider);
     capsColliderYBase = capsCollider.center.y;
 }
 
 function Update()
 {
     // grab the current animator state
     animStateBase = animator.GetCurrentAnimatorStateInfo(0); // 0 = BaseLayer
     
     // check if already jumping
     if (animStateBase.nameHash == stateJump)
     {
         // get the jump animation's time passed and use it as X value for reading the related curve Y value
         var animTime:float = this.animStateBase.normalizedTime;
         var curveValue:float = colliderYCurve.Evaluate(animTime);
         
         // ... then apply that value to the collider
         capsCollider.center.y = capsColliderYBase * curveValue;
 }


Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

OnTriggerStay2D Breaks When Adding AnimationController 1 Answer

collider animation problem 1 Answer

How To Play Animation 1 Answer

2D Animation does not start 1 Answer

Changing the default tangent mode for editing animation curves in Unity 2018.3 0 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