IK動畫
usingUnityEngine;
usingSystem.Collections;
publicclassPlayerIKScript:MonoBehaviour{
private Animator animator;
public? Transform? rightHandTarget;//右手子節(jié)點參考目標(biāo)
public? Transform? rightHand;//右手位置
private bool? isActive;//是否開始IK動畫
voidStart( ){
//得到動畫的控制器組件
animator=GetComponent<Animator>( );
}
voidUpdate( ){
if(Input.GetKeyDown(KeyCode.C)){
isActive=!isActive;
}
}
//IK動畫的回調(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)點來旋轉(zhuǎn)和移動父骨骼節(jié)點
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組件喂分,使用該組件來控制動畫狀態(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動畫參數(shù)的值為1
//animator.SetInteger(walk,1);
//}
//
////從walk狀態(tài)到run狀態(tài)
//if(Input.GetKeyDown(KeyCode.R)){
////設(shè)置walk動畫參數(shù)的值為2
//animator.SetInteger(walk,2);
//}
////從idel狀態(tài)到run狀態(tài)
//if(Input.GetKeyDown(KeyCode.P)){
////設(shè)置run動畫參數(shù)的值為1
//animator.SetInteger("Run",1);
//}
////從walk狀態(tài)到idel狀態(tài)
//if(Input.GetKeyDown(KeyCode.I)){
////設(shè)置walk動畫參數(shù)的值為0
//animator.SetInteger(walk,0);
//}
//if(Input.GetKeyDown(KeyCode.R)){
//if(animator.GetCurrentAnimatorStateInfo(0).IsName("Idle")){
//
//animator.SetInteger(run,1);
//
//
//}
//}
////判斷當(dāng)前是否在跑動狀態(tài)
//if(animator.GetCurrentAnimatorStateInfo(0).IsName("RunTurn")){
//
//animator.SetFloat(runTurn,Input.GetAxis("Horizontal"));
//
//}
//點擊W走動
//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)前動畫是否是滾動狀態(tài)
if(animator.GetCurrentAnimatorStateInfo(0).IsName("Roll")){
//獲取動畫曲線的值
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混合通常是指兩個參數(shù)來控制字動畫的混合剂公,2D的混合也可以分為三種不同模式谎砾,不同的模式下不同的應(yīng)用場合,他們的區(qū)別在于計算每個片段影響的具體方式澳迫,下面來詳解一下:
2Dsimple directional (2D簡單定向模式下)谆级,這種混合模式適用于所有的動畫都具有一定的運動方向罢浇,但其中任何兩段動畫運動方向都不相同的情形,例如扛门,向前走鸠信,向后走,向左走和向右走论寨,在次模式下星立,每一個方向上都不應(yīng)存在多段動畫,例如你想前走和向右跑是不能同時存在的葬凳,特別的绰垂,此時還可以存在一段處于(0,0)位置的特殊動畫,例如idle狀態(tài)火焰,當(dāng)然也可以不存在劲装。2Dfreeform directional (2D自由定向模式),這種混合模式同樣適用于所有的動畫都具有一定的運動方向的情形昌简,但是在同一方向上可以存在多段動畫占业,例如向前走和向前跑可以同時存在,特別的纯赎,在此模式下谦疾,必須存在一段(0,0)位置的動畫,例如idle狀態(tài)犬金。
2Dfreeform Cartesian(2D自由笛卡爾模式)念恍,這種混合模式使用于動畫部確定運動方向的情形,例如向左走然后又想右轉(zhuǎn)晚顷,向前跑然后右轉(zhuǎn)峰伙,在此模式下,X參數(shù)和Y參數(shù)可以代表不同的含義音同,例如角速度和線速度词爬。