Пример #1
0
    // Update is called once per frame
    void Update()
    {
        windowWidth = Input.mousePosition.x - transform.position.x;

        // Rotate character
        playerPosX = (int)transform.position.x;
        GetComponent <HealthBar>().DistanceChange = playerPosX;
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector3 moveDir  = (mousePos - transform.Find("CameraFollow").position);

        cameraFollowPosX = transform.Find("CameraFollow").position.x;

        if (differenceX > changeDirOffset && !facingRight)
        {
            Vector3 theScale = transform.localScale;
            theScale.x          *= -1;
            transform.localScale = theScale;
            facingRight          = true;
            ArmRotation.changeDir("Right");
            Vector3 oldPos = transform.Find("Karakter_3").position;
            oldPos.x -= posChange;
            transform.Find("Karakter_3").position = oldPos;
        }
        else if (differenceX < -changeDirOffset && facingRight)
        {
            Vector3 theScale = transform.localScale;
            theScale.x          *= -1;
            transform.localScale = theScale;
            facingRight          = false;
            ArmRotation.changeDir("Left");
            Vector3 oldPos = transform.Find("Karakter_3").position;
            oldPos.x += posChange;
            transform.Find("Karakter_3").position = oldPos;
        }


        // Movement
        weight = PlayerVariables.health;
        speed  = (speedForce - ((speedForce - 1) * weight / 100));
        if (Input.GetKey(KeyCode.A))
        {
            GetComponent <Rigidbody2D>().velocity = new Vector2(-(speedForce - (4 * weight / 100)), GetComponent <Rigidbody2D>().velocity.y);
            isMoving = true;
            transform.Find("Karakter_3").GetComponent <Animator>().SetBool("isWalking", true);
            transform.Find("Karakter_2").GetComponent <Animator>().SetBool("isWalking", true);
            transform.Find("Karakter_1").GetComponent <Animator>().SetBool("isWalking", true);
            transform.Find("Karakter_0").GetComponent <Animator>().SetBool("isWalking", true);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            GetComponent <Rigidbody2D>().velocity = new Vector2(speedForce - (4 * weight / 100), GetComponent <Rigidbody2D>().velocity.y);
            isMoving = true;
            transform.Find("Karakter_3").GetComponent <Animator>().SetBool("isWalking", true);
            transform.Find("Karakter_2").GetComponent <Animator>().SetBool("isWalking", true);
            transform.Find("Karakter_1").GetComponent <Animator>().SetBool("isWalking", true);
            transform.Find("Karakter_0").GetComponent <Animator>().SetBool("isWalking", true);
        }
        else
        {
            GetComponent <Rigidbody2D>().velocity = new Vector2(0, GetComponent <Rigidbody2D>().velocity.y);
            isMoving = false;
            transform.Find("Karakter_3").GetComponent <Animator>().SetBool("isWalking", false);
            transform.Find("Karakter_2").GetComponent <Animator>().SetBool("isWalking", false);
            transform.Find("Karakter_1").GetComponent <Animator>().SetBool("isWalking", false);
            transform.Find("Karakter_0").GetComponent <Animator>().SetBool("isWalking", false);
        }



        /* The old (commented) isGrounded were to slow to react so that double jumping could occure without enabling the timer, so it has now been updated to a more responsive version */
        // isGrounded = Physics2D.Linecast(this.transform.position, new Vector2(transform.position.x, transform.position.y - length), ground);// OverlapCircle (transform.position, radius, ground);
        velocityY  = GetComponent <Rigidbody2D>().velocity.y;
        isGrounded = velocityY <1e-8 && velocityY> -1e-8;


        /*
         * Jumping - Double jump every 5 seconds
         * Minor glitch where velocity.y is around 6-7*e-9 or something after double jump or while moving
         */
        if (doubleJumpCDTimer > 0.0f)
        {
            doubleJumpCDTimer -= Time.deltaTime;
        }
        if (doubleJumpCDTimer < 0.0f)
        {
            doubleJumpCDTimer = 0.0f;
        }
        // Single jump
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            GetComponent <Rigidbody2D>().AddForce(jumpVector, ForceMode2D.Force);
        }
        // Double jump
        if (Input.GetKeyDown(KeyCode.Space) && !isGrounded && doubleJumpCDTimer == 0.0f)
        {
            Debug.Log("DOUBLE JUMP");
            doubleJumpCDTimer = doubleJumpCDDuration;
            GetComponent <Rigidbody2D>().AddForce(doubleJumpVector, ForceMode2D.Force);
            HealthBar.startDoubleJumpCD(doubleJumpCDDuration);
        }


        /* Sprint */
        if (sprintCDTimer > 0.0f)
        {
            sprintCDTimer -= Time.deltaTime;
        }

        if (sprintCDTimer < 0.0f)
        {
            sprintCDTimer = 0.0f;
        }
        // sprint activate
        if (Input.GetKeyDown(KeyCode.LeftShift) && sprintCDTimer == 0.0f)
        {
            isSprinting   = true;
            sprintTimer   = sprintDuration;
            sprintCDTimer = sprintCDDuration;
            oldSpeed      = speedForce;
            speedForce   *= 1.5f;
            HealthBar.startSprintCD(sprintCDDuration);
        }

        if (isSprinting)
        {
            sprintTimer -= Time.deltaTime;
            if (sprintTimer < 0.0f)
            {
                isSprinting = false;
                speedForce  = oldSpeed;
            }
        }

        // Shooting
        if (Input.GetKey(KeyCode.Mouse0))
        {
            if (!shootOnCD)
            {
                source.PlayOneShot(magen, vol);
                StopCoroutine(Fire());
                StartCoroutine(Fire());
            }
        }

        // Food consumption
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            if (PlayerVariables.burgerCount > 0 && PlayerVariables.health != 100)
            {
                Debug.Log("Right Mouse Down " + PlayerVariables.burgerCount);
                HealthBar.eatBurger();
                PlayerVariables.changeBurgerCount(-1);
            }
        }



        differenceX = moveDir.x;
    }
Пример #2
0
 void OnTriggerStay2D(Collider2D other)
 {
     // Debug.Log("Ontrigger: " + other.tag);
     if (other.tag == "Damage")
     {
         // Debug.Log("Taking Damage onCD: " + onCD  + "  currentHealth: " + currentHealth);
         if (!onCD && currentHealth > 0)
         {
             // Debug.Log("IF !oncd and currenHEalth");
             StartCoroutine(CoolDownDmg());
             CurrentHealth -= 1;
         }
         Destroy(other.transform.gameObject);
     }
     if (other.tag == "food")
     {
         /*
          * Changed the entire functionality of burgers
          * // Debug.Log("Getting Healed" + onCD  + "  currentHealth: " + currentHealth);
          * if (!onCD && currentHealth < maxHealth)
          * {
          *  // Debug.Log("IF !oncd and currenhealth less than maxhealth: " + other.transform.gameObject);
          *  source.PlayOneShot(smatt, vol);
          *  if ((CurrentHealth + foodPlus) > 100)
          *  {
          *      CurrentHealth = 100;
          *  } else
          *  {
          *  CurrentHealth += foodPlus;
          *  }
          *  Destroy(other.transform.gameObject);
          * }
          * Destroy(other.transform.gameObject);
          */
         if (PlayerVariables.burgerCount < 5)
         {
             Debug.Log("Burger picked up");
             PlayerVariables.changeBurgerCount(1);
         }
         Destroy(other.transform.gameObject);
     }
     if (other.tag == "alcohol")
     {
         // Debug.Log("Getting Drunk" + onCD + "  currentHealth: " + currentHealth);
         if (playerVar.powerUpCD == false)
         {
             source.PlayOneShot(smatt, vol);
             // Debug.Log("POWERUP!!");
             HandlePowerUp();
             CurrentmadeFat += madeFat;
             Destroy(other.transform.gameObject);
         }
         Destroy(other.transform.gameObject);
     }
     if (other.tag == "carrot")
     {
         // Debug.Log("Damage" + onCD + "  currentHealth: " + currentHealth);
         Debug.Log("Carrot height: " + other.transform.position.y);
         if (!onCD && other.transform.position.y > 1.4)
         {
             // Debug.Log("IF !oncd and currenhealth less than maxhealth: " + other.transform.gameObject);
             source.PlayOneShot(smatt, vol);
             StartCoroutine(CoolDownDmg());
             CurrentHealth -= carrotMinus;
             Destroy(other.transform.gameObject);
         }
     }
 }