當前位置: 首頁>>代碼示例>>C#>>正文


C# Animator.MatchTarget方法代碼示例

本文整理匯總了C#中UnityEngine.Animator.MatchTarget方法的典型用法代碼示例。如果您正苦於以下問題:C# Animator.MatchTarget方法的具體用法?C# Animator.MatchTarget怎麽用?C# Animator.MatchTarget使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在UnityEngine.Animator的用法示例。


在下文中一共展示了Animator.MatchTarget方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: FixedUpdate

    private AnimatorStateInfo layer2CurrentState; // a reference to the current state of the animator, used for layer 2

    #endregion Fields

    #region Methods

    void FixedUpdate()
    {
        anim = GetComponent<Animator>();
        float h = Input.GetAxis("Horizontal");				// setup h variable as our horizontal input axis
        float v = Input.GetAxis("Vertical");				// setup v variables as our vertical input axis
        anim.SetFloat("Speed", v);							// set our animator's float parameter 'Speed' equal to the vertical input axis
        anim.SetFloat("Direction", h); 						// set our animator's float parameter 'Direction' equal to the horizontal input axis
        anim.speed = animSpeed;								// set the speed of our animator to the public variable 'animSpeed'
        anim.SetLookAtWeight(lookWeight);					// set the Look At Weight - amount to use look at IK vs using the head's animation
        currentBaseState = anim.GetCurrentAnimatorStateInfo(0);	// set our currentState variable to the current state of the Base Layer (0) of animation

        if(anim.layerCount ==2)
            layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1);	// set our layer2CurrentState variable to the current state of the second Layer (1) of animation

        // LOOK AT ENEMY

        // if we hold Alt..
        if(Input.GetButton("Fire2"))
        {
            // ...set a position to look at with the head, and use Lerp to smooth the look weight from animation to IK (see line 54)
            anim.SetLookAtPosition(enemy.position);
            lookWeight = Mathf.Lerp(lookWeight,1f,Time.deltaTime*lookSmoother);
        }
        // else, return to using animation for the head by lerping back to 0 for look at weight
        else
        {
            lookWeight = Mathf.Lerp(lookWeight,0f,Time.deltaTime*lookSmoother);
        }

        // STANDARD JUMPING

        // if we are currently in a state called Locomotion (see line 25), then allow Jump input (Space) to set the Jump bool parameter in the Animator to true
        /*if (currentBaseState.nameHash == locoState)
        {*/
            if(Input.GetButtonDown("Jump"))
            {
                anim.SetBool("Jump", true);
            }
        //}

        // if we are in the jumping state...
        else if(currentBaseState.nameHash == jumpState)
        {
            //  ..and not still in transition..
            if(!anim.IsInTransition(0))
            {
                /*if(useCurves)
                    // ..set the collider height to a float curve in the clip called ColliderHeight
                    col.height = anim.GetFloat("ColliderHeight");*/

                // reset the Jump bool so we can jump again, and so that the state does not loop
                anim.SetBool("Jump", false);
            }

            // Raycast down from the center of the character..
            Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
            RaycastHit hitInfo = new RaycastHit();

            if (Physics.Raycast(ray, out hitInfo))
            {
                // ..if distance to the ground is more than 1.75, use Match Target
                if (hitInfo.distance > 1.75f)
                {

                    // MatchTarget allows us to take over animation and smoothly transition our character towards a location - the hit point from the ray.
                    // Here we're telling the Root of the character to only be influenced on the Y axis (MatchTargetWeightMask) and only occur between 0.35 and 0.5
                    // of the timeline of our animation clip
                    anim.MatchTarget(hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(0, 1, 0), 0), 0.35f, 0.5f);
                }
            }
        }

        // JUMP DOWN AND ROLL

        // if we are jumping down, set our Collider's Y position to the float curve from the animation clip -
        // this is a slight lowering so that the collider hits the floor as the character extends his legs
        else if (currentBaseState.nameHash == jumpDownState)
        {
            col.center = new Vector3(0, anim.GetFloat("ColliderY"), 0);
        }

        // if we are falling, set our Grounded boolean to true when our character's root
        // position is less that 0.6, this allows us to transition from fall into roll and run
        // we then set the Collider's Height equal to the float curve from the animation clip
        else if (currentBaseState.nameHash == fallState)
        {
            col.height = anim.GetFloat("ColliderHeight");
        }

        // if we are in the roll state and not in transition, set Collider Height to the float curve from the animation clip
        // this ensures we are in a short spherical capsule height during the roll, so we can smash through the lower
        // boxes, and then extends the collider as we come out of the roll
        // we also moderate the Y position of the collider using another of these curves on line 128
        else if (currentBaseState.nameHash == rollState)
//.........這裏部分代碼省略.........
開發者ID:Jeace,項目名稱:ADGLADIUM,代碼行數:101,代碼來源:BotControlScript.cs


注:本文中的UnityEngine.Animator.MatchTarget方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。