Moving bones within a playing animation?

I’m struggling to find the fix for my code.

I have a character that is assigned on a 2d plane, think side scroller. When a key is pressed, the animations and movement for the character are stopped. This then triggers the aim script, which is assigned to the characters lower spine bone.

Once the aim script is triggered, all player control scripts are stopped (including animations) The low spine aiming script then tells the character to assume the crouch animation, and hold it until the key has been released.

Heres the problem. Because this crouch animation is playing, the player is unable to use the aim function; which consists of moving the mouse along the y axis to cause the bones to rotate up or down. Every time the mouse is moved, the bones move slightly but are reset to the default animation shape.

Anyway to overide the animations default bone state but keep the character crouched? Thanks, terra.

Here is the aim code:

var rotationSpeed = 300.0; 
var isEnabled : boolean = false;

var minAngle = -45; 
var maxAngle = 45; 

function LateUpdate () { 

 if (!Input.GetMouseButton (1))
	isEnabled = false;
 
 else if (!Input.GetMouseButtonUp (1))
	isEnabled = true;


//localEulerAngles prevent bone snapping, eulerAngles will snap the bones
//Following mouse movement, the bones will move when holding right mouse button

	if (isEnabled) 
	{
	
	//stop the dynamic player animations
	GameObject.Find("convict").GetComponent("PlatformerPlayerAnimation").enabled = false;

	//stop character movement
	GameObject.Find("convict").GetComponent("PlatformerController").enabled = false;
	
	//Stop physics collisions
	GameObject.Find("convict").GetComponent("PlatformerPushBodies").enabled = false;
	
	//set the character to crouching
	GameObject.Find("convict").animation.CrossFade ("Crouch");
		GameObject.Find("convict").animation["Walk"].wrapMode = WrapMode.ClampForever;
		
	
															
transform.localEulerAngles.y += Input.GetAxis("Mouse Y") * Time.deltaTime * rotationSpeed; 

     // Limit between 90 and 180 degrees 
     //transform.localEulerAngles.y = Mathf.Clamp(transform.localEulerAngles.y, minAngle, maxAngle); 
	var angle = transform.localEulerAngles.y; 
if (angle > 180) 
    angle -= 360; 
if (angle <= -180) 
    angle += 360; 
transform.localEulerAngles.y = Mathf.Clamp(angle, minAngle, maxAngle);
}


if (!Input.GetMouseButton (1)){
GameObject.Find("convict").GetComponent("PlatformerPlayerAnimation").enabled = true;
GameObject.Find("convict").GetComponent("PlatformerController").enabled = true;
GameObject.Find("convict").GetComponent("PlatformerPushBodies").enabled = true;
}
}

I’m looking for the same exact thing, I tried just leaving all the bones that are moved by the aim script unanimated but that made no difference. Anyway, if I find anything out I’ll post it here.

You may want to completely blend out your Walk

GameObject.Find(“convict”).GetComponent(Animation).Blend(“Walk”,0,0.1)

instead of clamping it forever to the last frame. That -might- be your problem.

If you still have bone movement where you don’t want it and you’re using Blender, try replacing your export_fbx.py with this one (changing .txt to .py). It goes in the Blender/.blender/scripts folder.
I had to rewrite it to correctly not export bones with no animation keys.

If you’re not using Blender… I don’t know how extensible other modelers are, but you might try looking for different versions of their exporters if there are such things.

p.s. - those Finds and GetComponents are more expensive than necessary; if your program starts to bog down later consider replacing them with variables that get set in the Start() function.

361796–12563–$export_fbx_476.txt (106 KB)

Thanks, I’ll see if that works for me. Do you know if I just save rotation data in a key frame in blender on some of the bones, like the arm bones that aim the guns up, will they still be able to move when I control the main bone that I actually move to aim the guns around? I’ll play around with it and see if I can get it to work.

Cool, exporting to a .fbx in the new blender 2.5 beta worked great, now if only I can figure out why my one guys arm is twisted weird, otherwise it works like a charm.

I haven’t tried my script with the 2.5 beta so it might have issues. Check your bone’s Roll in edit mode maybe?

I’m not sure if Unity handles rotation/transform separately, but it probably does. The exporter -should- correctly export that; if you only use Rotation keys in Blender, you should only get rotation animation in unity. But I haven’t tested it.

I do know that any bones with no keys can be fully controlled. :slight_smile: You can rotate, say, the upper arm, and control just the lower arm’s rotation.

Hey Astrauk,

When I’ve had rotation issues it was because my eularAngels, I switched to localEularAngels and it worked perfect. Not sure if this will help but It’s the little I know!

Haven’t had a chance to try any of this yet but I’ll get on it when I get some free time. Thanks.