TurnKey m_key;   // Only used for "windup" AI penguin

    // Initialize penguin's movement
    //
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        m_thisPenguin = animator.GetComponent<GamePenguin>();

        m_bTileSink = true;

        m_thisPenguin.MoveProgress = 0.0f;   // Start progress from one tile to another

        m_fTotalDistance = m_thisPenguin.DesiredDistance;   // Store distance

        // Set motion speed for penguin
        //
        m_thisPenguin.CurrentMoveSpeed = m_thisPenguin.DistanceToMoveSpeed(m_fTotalDistance);

        // Set key script to animate if this is an AI penguin
        //
        if (m_thisPenguin.IsAI)
        {
            m_key = m_thisPenguin.GetComponent<TurnKey>();

            m_key.KeyAnimating = true;
        }
        
        Debug.Log("Distance on this move: " + m_fTotalDistance);
	}
Beispiel #2
0
 private void Start()
 {
     key = SelectManeuver.Instance.GetComponent <TurnKey>();
 }
    void UpdateSmoothedMovementDirection()
    {
        //Transform cameraTransform= Camera.main.transform;
        bool grounded= IsGrounded();

        bool wasMoving= isMoving;

        // Grounded controls
        if (grounded)
        {
            // Lock camera for short period when transitioning moving & standing still
            lockCameraTimer += Time.deltaTime;
            if (isMoving != wasMoving)
                lockCameraTimer = 0.0f;
        }
        // In air controls
        else
        {
            if (jumping){
                if(_turnkey == TurnKey.down){//kill monster
                    gravity=500;
                    _turnkey = TurnKey.Idle;
                }
                else if((_turnkey == TurnKey.up)  && FlipFront ){
                    //doFlipWall();
                }
                else if((_turnkey == TurnKey.right)  && FlipRight){
                    //turnRL();
                    //doFlipWall();
                }
                else if((_turnkey == TurnKey.left)  && FlipLeft){
                    //turnRL();
                    //doFlipWall();
                }
            }//end jumping
            else {
                gravity=20;
            }
            if (isMoving)
                inAirVelocity += moveDirection * Time.deltaTime * inAirControlAcceleration;
        }
    }
    void Update()
    {
        if(ScriptCtrl.useSkill){
            if(Time.time > ScriptCtrl.SkillFinishTime){
                ScriptCtrl.useSkill = false;
                //FlickerTimes=0;
                print("end");
            }

            if(ScriptCtrl.useSkill && (Time.time > ScriptCtrl.SkillFinishTime-4f) && Time.time > nextFlicker){
                nextFlicker = Time.time+0.2f;
                //FlickerTimes++;
                skillFlicker = !skillFlicker;
                NGUITools.SetActive(Skill,skillFlicker);
                print ("flicker");
            }
            else if(ScriptCtrl.useSkill && (Time.time < ScriptCtrl.SkillFinishTime-3f)){
                NGUITools.SetActive(Skill,true);
                speed = 14f;
                print("start");
            }
            else if(!ScriptCtrl.useSkill){
                NGUITools.SetActive(Skill,false);
                speed = 7f;
                print("dieable");
            }

        }
        if (!isControllable)
        {
            // kill all inputs if not controllable.
            Input.ResetInputAxes();
        }
        if(speed>0 && !jumping)
        {
            _characterState = CharacterState.Running;
        }
        RaycastHit hit ;
        //detect wall is fornt or left or right then enable flipWall

        if(Physics.Raycast(transform.position, this.transform.forward, out hit, 3.0f)){//front
            if(hit.transform.gameObject.tag == "Wall")  {FlipFront=true;}
            else 										{FlipFront=false;}
            Debug.DrawLine(transform.position, hit.transform.position , Color.green);
        }

        if(Physics.Raycast(transform.position, this.transform.right, out hit, 3.0f)){//right
            if(hit.transform.gameObject.tag == "Wall")  {FlipRight=true;}
            else 										{FlipFront=false;}
            Debug.DrawLine(transform.position, hit.transform.position , Color.green);
        }

        if(Physics.Raycast(transform.position, -this.transform.right, out hit, 3.0f)){//left
            if(hit.transform.gameObject.tag == "Wall")  {FlipLeft=true;}
            else										{FlipFront=false;}
            Debug.DrawLine(transform.position, hit.transform.position , Color.green);
        }

        if(getKeyTime <Time.time-1.3){//release turn gesture
            _turnkey = TurnKey.Idle;
        }

        _turnkey = getTurnKey();
        /*
        //----------------------------------move-------------------------------------
        if(Input.GetKeyDown(KeyCode.W)){
            _turnkey = TurnKey.up;
            //getKeyTime= Time.time;
        }
        if(Input.GetKeyDown(KeyCode.S) && !jumping){
            _turnkey = TurnKey.down;
            //getKeyTime= Time.time;
        }
        if(Input.GetKeyDown(KeyCode.D)){
            _turnkey = TurnKey.right;
            getKeyTime= Time.time;//enable turn right and left in the duration
        }
        if(Input.GetKeyDown(KeyCode.A)){
            _turnkey = TurnKey.left;
            getKeyTime= Time.time;
        }
        if(Input.touchCount>0){
            Touch touch =Input.touches[0];switch(touch.phase){
                case TouchPhase.Began:
                    startPos = touch.position;
                    break;
                case TouchPhase.Moved:
                    if(Mathf.Abs(touch.position.y - startPos.y) > 100f)
                    {
                        float swipValue = Mathf.Sign(touch.position.y - startPos.y);
                        if(swipValue > 0){
                            _turnkey = TurnKey.up;
                            getKeyTime= Time.time;
                            break;
                        }
                        else if(swipValue < 0  &&  !jumping){
                            _turnkey = TurnKey.down;
                            getKeyTime= Time.time;
                            break;
                        }
                    }

                    else if(Mathf.Abs(touch.position.x - startPos.x) > 100f)
                    {
                        float swipValue = Mathf.Sign(touch.position.x - startPos.x);
                        if(swipValue > 0){
                            _turnkey = TurnKey.right;
                            getKeyTime= Time.time;
                            break;
                        }
                        else if(swipValue < 0){
                            _turnkey = TurnKey.left;
                            getKeyTime= Time.time;
                            break;
                        }
                    }
                break;
            }

        }//---------------------------------move end----------------------------------------
        else{
            turnUpOrDown();
        }*/

        if (Input.GetButtonDown ("Jump"))
        {
            lastJumpButtonTime = Time.time;
        }

        UpdateSmoothedMovementDirection();

        // Apply gravity
        // - extra power jump modifies gravity
        // - controlledDescent mode modifies gravity
        ApplyGravity ();

        // Apply jumping logic
        ApplyJumping ();

        // Calculate actual motion
        Vector3 movement= moveDirection * speed + new Vector3 (0, verticalSpeed, 0) + inAirVelocity;
        movement *= Time.deltaTime;
        // Move the controller
        CharacterController controller = GetComponent<CharacterController>();
        collisionFlags = controller.Move(movement);

        // ANIMATION sector
        if(_animation) {
            if(_characterState == CharacterState.Jumping)
            {
                if(!jumpingReachedApex) {
                    _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
                    _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
                    _animation.CrossFade(jumpPoseAnimation.name);
                } else {
                    _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
                    _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
                    _animation.CrossFade(jumpPoseAnimation.name);
                }
            }
            else
            {
                if(_characterState == CharacterState.Dying) {
                    _animation.CrossFade(dieAnimation.name);
                }
                else if(controller.velocity.sqrMagnitude < 0.1f) {
                    _animation.CrossFade(idleAnimation.name);
                }
                else
                {
                    if(_characterState == CharacterState.Running) {
                        _animation[runAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f, runMaxAnimationSpeed);
                        _animation.CrossFade(runAnimation.name);
                    }
                    else if(_characterState == CharacterState.Dying) {
                        _animation[dieAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
                        _animation.CrossFade(dieAnimation.name);
                    }

                }
            }
        }
        // ANIMATION sector

        // We are in jump mode but just became grounded
        if (IsGrounded())
        {
            lastGroundedTime = Time.time;
            inAirVelocity = Vector3.zero;
            if (jumping)
            {
                jumping = false;
                SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
            }
        }
        //speed=0 can turn
        if(controller.velocity.sqrMagnitude < 0.1f){
            turnRL();
        }
    }
 void doFlipWall()
 {
     CharacterController controller = GetComponent<CharacterController>();
     RaycastHit hit;
     float oriY = transform.position.y;
     //transform.Translate(0,5f,0);
     //transform.Translate(0,0,3f);
     verticalSpeed = CalculateJumpVerticalSpeed (jumpHeight*1.2f);
     if(collide.collider.tag == "Wall"){
         hitTemp = collide;
         controller.detectCollisions = false;
         //hitTemp.collider.enabled = false;
     }
     flipAnimation();
     //Physics.IgnoreCollision(this.transform.collider,collide);
     if(Physics.Raycast(transform.position, (-transform.up+new Vector3(-1f,0,0))*100, out hit)){
         Debug.DrawRay(transform.position,(-transform.up+new Vector3(-1f,0,0))*100,Color.blue);
         if(hit.transform.tag == "ground"){
             //transform.Translate(0,-2f,0);
             gravity = 1000f;
             _turnkey = TurnKey.Idle;
             speed = 0;
             //hitTemp.collider.enabled = true;
             controller.detectCollisions = true;
             //transform.position.y = oriY;
         }else{
             //transform.Translate(0,0,0.001f);
         }
     }
     //hitTemp.collider.enabled = true;
 }
 public void turnUpOrDown()
 {
     switch(_turnkey){
         case TurnKey.up:
             lastJumpButtonTime = Time.time;    //apply jump
             grounded = false;
             _turnkey = TurnKey.Idle;
             break;
         case TurnKey.down:
             transform.Rotate(new Vector3(0f,1f,0f),180f);
             moveDirection=transform.TransformDirection(Vector3.forward);
             _turnkey = TurnKey.Idle;
             break;
     }
 }
 public void turnRL()
 {
     switch(_turnkey){
         case TurnKey.right:
             transform.Rotate(new Vector3(0f,1f,0f),90f);
             moveDirection=transform.TransformDirection(Vector3.forward);
             _turnkey = TurnKey.Idle;
             break;
         case TurnKey.left:
             transform.Rotate(new Vector3(0f,1f,0f),-90f);
             moveDirection=transform.TransformDirection(Vector3.forward);
             _turnkey = TurnKey.Idle;
             break;
     }
 }