Example #1
0
    //called by event in pistol_shot clip
    void Fire(SpriteAnimationEvent evt)
    {
        //if not hold shot button, then just leave
        if ( !Input.GetMouseButton(0) )
            return;

        //get the position of barrel in local space, event will fired when the all the compoents in clip transformed.
        SpriteTransform barrelTransform = spriteRenderer.GetSpriteTransform("/root/barrel");

        //the bullet spawn position in local space
        SpriteTransform bulletSpawnTransform = spriteRenderer.GetSpriteTransform("/root/barrel/bulletSpawnPosition");

        if ( barrelTransform != null && bulletSpawnTransform != null )
        {
            //transform two positions into world space
            Vector3 barrelPosition = transform.TransformPoint( barrelTransform.position );
            Vector3 bulletSpawnPosition = transform.TransformPoint( bulletSpawnTransform.position );

            //set they in same plane
            barrelPosition.z = 0f;
            bulletSpawnPosition.z = 0f;

            //angle of bullet, then it fired
            float bulletAngle = Mathf.Sign( bulletSpawnPosition.y - barrelPosition.y ) * Vector3.Angle( (bulletSpawnPosition - barrelPosition), Vector3.right);

            //Instantiate the bullet with the position and the rotation
            GameObject bullet = Object.Instantiate( bullecPrototype, bulletSpawnPosition, Quaternion.Euler(0f,0f,bulletAngle) ) as GameObject;

            //set the bullet movement direction
            bullet.GetComponent<Bullet>().direction = (bulletSpawnPosition - barrelPosition).normalized;
        }
    }
        private void FireAnimationEvent(SpriteAnimationState state, int idx)
        {
            //计算当前时间与最后次更新的时间差
            float t = state.time - state.lastTime;
            //计算出步进了几个tick
            int step = (int)(t / state._clip.tick);
            //保存起始的帧索引
            int li = state.lastFrameIndex;
            //事件数组的长度
            int e = state._clip.events.Length;

            //搜索时间内的事件
            for (float st = state.lastTime; st < state.time; st += state._clip.tick)
            {
                //得到当前时间的索引
                int currFrameIdx = (int)state.curve.Evaluate(st);
                //检查是否和上次不一致,如果是则进行一个线性事件搜索
                if (li != currFrameIdx)
                {
                    for (int ei = 0; ei < e; ei++)
                    {
                        if (state._clip.events[ei].frameIndex == currFrameIdx)
                        {
                            SpriteAnimationEvent evt = state._clip.events[ei];
                            evtList.Add(evt);
                            break;
                        }
                    }
                }
                state.lastFrameIndex = li = currFrameIdx;
            }

            if (state.lastFrameIndex != idx)
            {
                for (int ei = 0; ei < e; ei++)
                {
                    if (state._clip.events[ei].frameIndex == idx)
                    {
                        SpriteAnimationEvent evt = state._clip.events[ei];
                        evtList.Add(evt);
                        break;
                    }
                }
            }
        }
 public static void SetAnimationClipEvents(SpriteAnimationClip clip, SpriteAnimationEvent[] events)
 {
     clip.events = events;
     clip.CalcEvents();
 }
Example #4
0
    //SpriteAnimation's event handler
    //Define two event in clip named Mummy_Attack
    //When you play this clip, that will call function StartActtack and EndAttack
    void StartAttack( SpriteAnimationEvent evt)
    {
        //get component's SpriteTransform named "hitbox"
        //we use the "hitbox"'s move track to do attacking raycast collision detection
        SpriteTransform tran = spriteRenderer.GetSpriteTransform("hitbox");

        //transform hitbox position from local space to world space
        Matrix4x4 trs = Matrix4x4.TRS(
            new Vector3(tran.position.x, tran.position.y),
            Quaternion.Euler(0, 0, tran.rotation),
            tran.scale);

        trs = transform.localToWorldMatrix * trs;
        atkHit.startPos = trs.MultiplyPoint( Vector3.zero );

        //fill attack hit info
        atkHit.currPos = atkHit.startPos;
        atkHit.direction.x = moveDirection;
        atkHit.attacker = gameObject;
        atkHit.attackNo++;
    }
Example #5
0
 void EndAttack( SpriteAnimationEvent evt)
 {
 }
Example #6
0
 void EndAttack( SpriteAnimationEvent evt)
 {
     isNeedAttackcollisionDetect = false;
 }
 //
 public void EndAttack(SpriteAnimationEvent evt)
 {
     doAttackCollisionDetect = false;
 }
    //Attack handler, here just called by event in animation clip.
    //Usually, you should do collision detection here, or call this function after objects have collision.
    public void StartAttack(SpriteAnimationEvent evt)
    {
        doAttackCollisionDetect = true;

        if (moveTarget.targetObject.moveTarget != null)
            moveTarget.targetObject.moveTarget = new MoveTarget(this);

        //target get damage
        moveTarget.targetObject.Damage();
    }