Playing animations with different inputs?

I am trying to get my character to be able to crossfade into his attack while running. The character runs with “d”, and attacks with “mouse 0”. I need the character to be able to use “mouse 0” while “d” is pressed and without it. Currently it works if the character isn’t running/moving, but will not crossfade/play if the character is moving. The
| | statement on line 58 is what I have tried and is not working. Thanks.

** The game is a 2.5D sidescroller, with basic WASD movement, and attacks with mouse 0 1 **

CURRENT CODE

// Player Script : Movement
     
     
     
 // Inspector Variables
     
var playerSpeed : float = 5.0;				// Players Speed
private var moveDirection : int = 0; 		// Decides which way character is facing
private var isIdle : boolean = true; 
var rightShoulder : Transform;
   
function Update () {
     
// Clamp Rotation to 90
     

// Character Movement
     
// RIGHT
     
if (Input.GetButton ("Move Right"))
{
   moveDirection = 0;
   transform.Translate (0, 0, playerSpeed * Time.deltaTime);
 }
     
// LEFT
     
if (Input.GetButton ("Move Left"))
{
    moveDirection = 1;
    transform.Translate (0, 0, playerSpeed * Time.deltaTime);
}

     
     
// JUMP
     
if (Input.GetButtonDown ("Jump2"))
{
    rigidbody.velocity = Vector3 (0, 12, 0);     // Jumps in air
    rigidbody.velocity.y -= 4;
 }

// Animations  Attacks 

if (rigidbody.velocity.z > 0)					// Run Right
{
    animation.CrossFade("CRun");
}

else if (rigidbody.velocity.z < 0)				//Run Left
{
    animation.CrossFade("CRun");
}

else if (Input.GetButtonDown ("Swing One"))     //Swing1 Attack
{
	Swing1();
}

if (rigidbody.velocity.z == 0  isIdle == true)   // Idle
{
	animation.CrossFade("CIdle");
}
 
 
 // Animation MIXING
 
 var runIsPlaying : boolean;
 
 if (animation.IsPlaying ("CRun"))
{
 	runIsPlaying = true;
}	
 	
 if (Input.GetButtonDown ("Swing One")  runIsPlaying == true)   		// Swing while running
{
	animation["CSwing1"].AddMixingTransform(rightShoulder);
} 


	

// Variable setup for eulerAngle assignment used on LEFT and RIGHT movement
	
if (moveDirection == 1)  // Left
{
	transform.eulerAngles = Vector3(0, -90, 0);
}	

if (moveDirection == 0)  // Right
{
	transform.eulerAngles = Vector3(0, 90, 0);
}	
}

// Adjust character sliding problem

if (transform.position.z > -1)
{
	transform.position.z = -1;
}

if (transform.position.z < -1)
{
	transform.position.z = -1;
}

// Function that sets up Swing1 Delay
function Swing1 ()
{
	isIdle = false;
	animation.CrossFade("CSwing1");
	yield WaitForSeconds (animation["CSwing1"].length);
	isIdle = true;
	
	
}

if you want your character to be animated running and attacking at the same time you have to blend animations. look into script reference for addmixingtransform and animation.blend…also a quick note: animations in unity do have weights (min 0.0f, max 1.0f), so everytime you use crossfade the currently played animations weight is lowered down while the new animations weight is increased until its weight is 1.0f.

Thanks for taking the time to post that information, it is exactly what I need I think. It makes sense to me from a logical standpoint, however I am not sure how to implement this with the code I already have. I have the run animation and the attack animation (swing1) playing from different input buttons. I can’t figure out for example how to add the swing attack to the run, and have that blend only when the character is already running? Confused from a syntax standpoint.

Can anybody show an example of how i can use apprentice method with my current code?

This is mixing the animation on the rightShoulder for when the attack is pressed and the character is not running. I am trying to get it working for when the character is running, and play the normal attack when the character is not running.

 var runIsPlaying : boolean;
 
 if (animation.IsPlaying ("CRun"))
{
 	runIsPlaying = true;
}	
 	
 if (Input.GetButtonDown ("Swing One")  runIsPlaying == true)   
{
	animation["CSwing1"].AddMixingTransform(rightShoulder);
}