- Home /
How do I add procedural muscular spasms to a Humanoid Rig? (electrocute, seizure)
Hi there!
I'm trying to get what I thought to be a simple position/rotation noise to the bones of a Humanoid Rig. I need it to simulate muscular spasms induced by a venom on a character, and the attempt of the character to resist. I would also like to recycle the effect to simulate the electrocution of a character. I'm pretty sure it can be done without third party plugins, so I'd prefer a solution efficient enough to run on mobile (single character)
I tried to write a StateMachineBehaviour, but, well, my hope that only animating the IK targets would result in something decent failed miserably.
Is there a way to use the muscles like those in the avatar? I have no idea on how to iterate each bone/muscle to apply some jitter.
[would be nice if the body stayed in place, I don't want the character to jump out of the camera during a cutscene :P ]
Here's what I wrote. Please note that I can successfully trigger it using Mecanim, but the effect itself is pretty lame :P
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimStateSeizure : StateMachineBehaviour
{
public float initialSeizureAmount;
public float targetSeizureAmount;
public float interpolationSpeed;
public float currentSeizureAmount;
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
currentSeizureAmount = initialSeizureAmount;
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
currentSeizureAmount =
Mathf.Lerp(currentSeizureAmount, targetSeizureAmount, interpolationSpeed * Time.deltaTime);
}
// OnStateIK is called right after Animator.OnAnimatorIK()
override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
// Implement code that sets up animation IK (inverse kinematics)
Spasm(animator, AvatarIKGoal.LeftFoot);
Spasm(animator, AvatarIKGoal.RightFoot);
Spasm(animator, AvatarIKGoal.LeftHand);
Spasm(animator, AvatarIKGoal.RightHand);
}
private void Spasm(Animator animator, AvatarIKGoal ikGoal)
{
animator.SetIKPosition(ikGoal, animator.GetIKPosition(ikGoal) + Random.insideUnitSphere * currentSeizureAmount);
animator.SetIKPositionWeight(ikGoal, 1f);
animator.SetIKRotation(ikGoal, Quaternion.FromToRotation(animator.GetIKRotation(ikGoal).eulerAngles, Random.insideUnitSphere * currentSeizureAmount));
animator.SetIKRotationWeight(ikGoal, 1f);
}
}
Your answer

Follow this Question
Related Questions
Problems with using animations for a model that has different proportions 0 Answers
Adjusting humanoid character animations (Procedural animations) 0 Answers
Blender Rigify humanoid animation importing into Unity with SIDEWAYS feet 0 Answers
How to fix Animator.SetLookAtPosition behaves differently with different animation clip? 0 Answers