5.9IK動(dòng)畫和動(dòng)畫曲線

IK動(dòng)畫

usingUnityEngine;

usingSystem.Collections;

publicclassPlayerIKScript:MonoBehaviour{

private Animator animator;

public? Transform? rightHandTarget;//右手子節(jié)點(diǎn)參考目標(biāo)

public? Transform? rightHand;//右手位置

private bool? isActive;//是否開始IK動(dòng)畫

voidStart( ){

//得到動(dòng)畫的控制器組件

animator=GetComponent<Animator>( );

}

voidUpdate( ){

if(Input.GetKeyDown(KeyCode.C)){

isActive=!isActive;

}

}

//IK動(dòng)畫的回調(diào)方法---前提是IKPass必須勾選

void? OnAnimatorIK(int LayerIndex ){

if(animator){

//設(shè)置骨骼的權(quán)重,1表示完整骨骼

animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);

animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);

if(isActive){

//設(shè)置骨骼的位置和旋轉(zhuǎn)

if(rightHandTarget){

//設(shè)置右手根據(jù)目標(biāo)點(diǎn)來旋轉(zhuǎn)和移動(dòng)父骨骼節(jié)點(diǎn)

animator.SetIKPosition(AvatarIKGoal.RightHand,Vector3.Lerp(rightHand.position,

rightHandTarget.position,0.5f));

animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandTarget.rotation);

}

}

}

}

}


usingUnityEngine;

usingSystem.Collections;

publicclassPlayerScript:MonoBehaviour{

private Animator animator;

//private int walk;

//private int run;

private int runTurn;

//private int walkTurn;

private CharacterController characterController;

voidStart( ){

//獲取animator組件,使用該組件來控制動(dòng)畫狀態(tài)

animator=GetComponent<Animator>( ) ;

characterController=GetComponent<CharacterController>( );

//轉(zhuǎn)換為hash值,效率更高

//walk=Animator.StringToHash("Walk");

//run=Animator.StringToHash("Run");

runTurn=Animator.StringToHash("RunTurn");

//walkTurn=Animator.StringToHash("WalkTurn");

}

voidUpdate(){

////從其他狀態(tài)到walk狀態(tài)

//if(Input.GetKeyDown(KeyCode.W)){

////設(shè)置walk動(dòng)畫參數(shù)的值為1

//animator.SetInteger(walk,1);

//}

//

////從walk狀態(tài)到run狀態(tài)

//if(Input.GetKeyDown(KeyCode.R)){

////設(shè)置walk動(dòng)畫參數(shù)的值為2

//animator.SetInteger(walk,2);

//}

////從idel狀態(tài)到run狀態(tài)

//if(Input.GetKeyDown(KeyCode.P)){

////設(shè)置run動(dòng)畫參數(shù)的值為1

//animator.SetInteger("Run",1);

//}

////從walk狀態(tài)到idel狀態(tài)

//if(Input.GetKeyDown(KeyCode.I)){

////設(shè)置walk動(dòng)畫參數(shù)的值為0

//animator.SetInteger(walk,0);

//}

//if(Input.GetKeyDown(KeyCode.R)){

//if(animator.GetCurrentAnimatorStateInfo(0).IsName("Idle")){

//

//animator.SetInteger(run,1);

//

//

//}

//}

////判斷當(dāng)前是否在跑動(dòng)狀態(tài)

//if(animator.GetCurrentAnimatorStateInfo(0).IsName("RunTurn")){

//

//animator.SetFloat(runTurn,Input.GetAxis("Horizontal"));

//

//}

//點(diǎn)擊W走動(dòng)

//if(Input.GetKeyDown(KeyCode.W)){

//

//if(animator.GetCurrentAnimatorStateInfo(0).IsName("Idle")){

//

//animator.SetInteger(walk,1);

//}

//}

//

//if(animator.GetCurrentAnimatorStateInfo(0).IsName("Walk")){

//

//animator.SetFloat(walkTurn,Input.GetAxis("Horizontal"));

//}

if(animator.GetCurrentAnimatorStateInfo(0).IsName("Move")){

animator.SetFloat("Speed",Input.GetAxis("Vertical"));

animator.SetFloat(runTurn,Input.GetAxis("Horizontal"));

}

if(Input.GetKeyDown(KeyCode.Space)){

animator.SetTrigger("Roll");

}

//動(dòng)畫曲線

//判斷當(dāng)前動(dòng)畫是否是滾動(dòng)狀態(tài)

if(animator.GetCurrentAnimatorStateInfo(0).IsName("Roll")){

//獲取動(dòng)畫曲線的值

characterController.height=animator.GetFloat("Height");

characterController.center=newVector3(characterController.center.x,animator.GetFloat("Y"),characterController.center.z);

}

if(Input.GetKeyDown(KeyCode.J)){

animator.SetTrigger("Jump");

}

if(Input.GetKeyDown(KeyCode.S)){

//animator.SetTrigger("Shoot");

animator.SetBool("Raise",true);

}

}

}

2D混合2D混合通常是指兩個(gè)參數(shù)來控制字動(dòng)畫的混合,2D的混合也可以分為三種不同模式,不同的模式下不同的應(yīng)用場(chǎng)合,他們的區(qū)別在于計(jì)算每個(gè)片段影響的具體方式,下面來詳解一下:

2Dsimple directional (2D簡(jiǎn)單定向模式下),這種混合模式適用于所有的動(dòng)畫都具有一定的運(yùn)動(dòng)方向,但其中任何兩段動(dòng)畫運(yùn)動(dòng)方向都不相同的情形,例如,向前走,向后走,向左走和向右走,在次模式下,每一個(gè)方向上都不應(yīng)存在多段動(dòng)畫,例如你想前走和向右跑是不能同時(shí)存在的,特別的,此時(shí)還可以存在一段處于(0,0)位置的特殊動(dòng)畫,例如idle狀態(tài),當(dāng)然也可以不存在。2Dfreeform directional (2D自由定向模式),這種混合模式同樣適用于所有的動(dòng)畫都具有一定的運(yùn)動(dòng)方向的情形,但是在同一方向上可以存在多段動(dòng)畫,例如向前走和向前跑可以同時(shí)存在,特別的,在此模式下,必須存在一段(0,0)位置的動(dòng)畫,例如idle狀態(tài)。

2Dfreeform Cartesian(2D自由笛卡爾模式),這種混合模式使用于動(dòng)畫部確定運(yùn)動(dòng)方向的情形,例如向左走然后又想右轉(zhuǎn),向前跑然后右轉(zhuǎn),在此模式下,X參數(shù)和Y參數(shù)可以代表不同的含義,例如角速度和線速度。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容