コード例 #1
0
 void StartDying()                                                              // some changes should be made when the zombie dies. This function exists because of that
 {
     transform.localScale = new Vector3((distance_among.x < 0) ? -1 : 1, 1, 1); //direction adjustment
     physic.bodyType      = RigidbodyType2D.Static;
     physic.GetComponent <PolygonCollider2D>().enabled       = false;
     physic.GetComponentInChildren <BoxCollider2D>().enabled = false;        // Disable zombie's ground checker collider
 }
コード例 #2
0
    /// <summary>
    /// Процесс атаки
    /// </summary>
    protected override IEnumerator AttackProcess()
    {
        Animate(new AnimationEventArgs("attack", crit ? "CritAttack": "Attack", 0));
        employment = Mathf.Clamp(employment - 8, 0, maxEmployment);
        yield return(new WaitForSeconds(crit ? preCritAttackTime : preAttackTime));

        Vector3 direction = (SpecialFunctions.player.transform.position - (transform.position +
                                                                           new Vector3(sightOffsetX * (int)orientation, sightOffsetY, 0f))).normalized;
        GameObject newCoal = Instantiate(coal, transform.position + new Vector3(sightOffsetX * (int)orientation, sightOffsetY, 0f),
                                         Quaternion.identity) as GameObject;
        Rigidbody2D coalRigid = newCoal.GetComponent <Rigidbody2D>();

        coalRigid.velocity = direction * coalSpeed;
        HitBox coalHitBox = coalRigid.GetComponentInChildren <HitBox>();

        if (coalHitBox != null)
        {
            coalHitBox.SetEnemies(enemies);
            coalHitBox.SetHitBox(new HitClass(crit? damage * 1.5f:damage, -1f, coalHitSize, Vector2.zero, coalForce));
        }
        employment = Mathf.Clamp(employment + 5, 0, maxEmployment);

        yield return(new WaitForSeconds(attackRate));

        employment = Mathf.Clamp(employment + 3, 0, maxEmployment);
    }
コード例 #3
0
        private void OnMouseDown()
        {
            _getsForce        = true;
            _renderer.enabled = true;
            var position = GetWorldPositionOnPlane(Input.mousePosition, _renderer.transform.position.z);

            _renderer.SetPosition(1, position);
            _renderer.startColor = _player.GetComponentInChildren <TrailRenderer>().material.color;
        }
コード例 #4
0
ファイル: PlayerMovement.cs プロジェクト: ArnarSnaer/CoopGame
    //private float transformWaitTime = 0.25f;


    // Start is called before the first frame update
    void Start()
    {
        startSpeed = speed;
        rb         = GetComponent <Rigidbody2D>();
        playerRb   = playerObj.GetComponent <Rigidbody2D>();
        platformRb = platformObj.GetComponent <Rigidbody2D>();
        platformSr = platformObj.GetComponent <SpriteRenderer>();
        zrb        = rb.GetComponentInChildren <Rigidbody2D>();
        tf         = GetComponent <Transform>();
    }
コード例 #5
0
    void CheckRigidbodyContacts(Rigidbody2D rb)
    {
        int contactCount = rb.GetContacts(contactFilter, contactPoints);

        for (int j = 0; j < contactCount; j++)
        {
            ContactPoint2D contactPoint2D   = contactPoints[j];
            Rigidbody2D    contactRigidbody = contactPoint2D.rigidbody == rb ? contactPoint2D.otherRigidbody : contactPoint2D.rigidbody;
            int            listIndex        = -1;

            for (int k = 0; k < caughtObjects.Count; k++)
            {
                if (contactRigidbody == caughtObjects[k].rigidbody)
                {
                    listIndex = k;
                    break;
                }
            }

            if (listIndex == -1)
            {
                if (contactRigidbody != null)
                {
                    if (contactRigidbody.bodyType != RigidbodyType2D.Static && contactRigidbody != platformRigidbody)
                    {
                        float dot = Vector2.Dot(contactPoint2D.normal, Vector2.down);
                        if (dot > 0.8f)
                        {
                            CaughtObject newCaughtObject = new CaughtObject
                            {
                                rigidbody        = contactRigidbody,
                                character        = contactRigidbody.GetComponent <CharacterController2D>(),
                                collider         = contactRigidbody.GetComponent <Collider2D>(),
                                inContact        = true,
                                checkedThisFrame = false
                            };

                            if (newCaughtObject.collider == null)
                            {
                                newCaughtObject.collider = contactRigidbody.GetComponentInChildren <Collider2D>();
                            }

                            caughtObjects.Add(newCaughtObject);
                        }
                    }
                }
            }
            else
            {
                caughtObjects[listIndex].inContact = true;
            }
        }
    }
コード例 #6
0
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.CompareTag("Reward"))
     {
         //播放吃金币音乐
         AudioSource.PlayClipAtPoint(crystal1, Camera.main.transform.position);
         rewardNum++;
         m_GameUIManager.UpdateScoreLabel(rewardNum); //更新UI分数显示.
         Destroy(collision.gameObject);
     }
     if (collision.gameObject.CompareTag("Bat"))
     {
         CatDead = true;
         AudioSource.PlayClipAtPoint(catdead, Camera.main.transform.position);
         //缺少蝙蝠AI以及猫死亡动画
         anim.SetBool("catDead", CatDead);
         Debug.Log("cat is deaded");
         Collider2D    c1 = rig.GetComponent <Collider2D>();
         BoxCollider2D c2 = rig.GetComponentInChildren <BoxCollider2D>();
         c1.enabled      = false;
         c2.enabled      = false;
         rig.constraints = RigidbodyConstraints2D.None;
     }
     if (collision.gameObject.CompareTag("Flame"))
     {
         CatDead = true;
         AudioSource.PlayClipAtPoint(catdead, Camera.main.transform.position);
         anim.SetBool("catDead", CatDead);
         Collider2D    c1 = rig.GetComponent <Collider2D>();
         BoxCollider2D c2 = rig.GetComponentInChildren <BoxCollider2D>();
         c1.enabled = false;
         c2.enabled = false;
     }
     if (collision.gameObject.CompareTag("batdead"))
     {
         anim.SetBool("grounded", false);
     }
 }
コード例 #7
0
    void OnTriggerEnter2D(Collider2D col)
    {
        Owner       myOwner      = gameObject.GetComponent <Owner> ();
        GameObject  damageSource = myOwner ? myOwner.owner : null;
        Rigidbody2D rb           = col.attachedRigidbody;

        if (rb)
        {
            Health health = rb.GetComponentInChildren <Health> ();
            if (health)
            {
                health.TakeDamage(damage, damageSource);
            }
        }
    }
コード例 #8
0
    protected TrashObject GetTrashObjectFromCollider(Collider2D collider)
    {
        Rigidbody2D rb = collider.attachedRigidbody;

        TrashObject trashObject = null;

        if (rb != null)
        {
            trashObject = rb.GetComponentInChildren <TrashObject> ();
        }

        if (trashObject == null)
        {
            trashObject = collider.gameObject.GetComponentInChildren <TrashObject> ();
        }

        return(trashObject);
    }
コード例 #9
0
    void OnTriggerEnter2D(Collider2D other)
    {
        if (!isHit)
        {
            isHit = true;
            if (rb2d == null)
            {
                rb2d = GetComponent <Rigidbody2D>();
            }
            rb2d.velocity = Vector2.zero;
            rb2d.bodyType = RigidbodyType2D.Kinematic;
            rb2d.GetComponentInChildren <CircleCollider2D>().enabled = false;

            StartCoroutine(TriggerHitAnimation());
            if (other.gameObject.tag == "Enemy")
            {
                other.gameObject.SendMessage("Damage", combatData);
            }
        }
    }
コード例 #10
0
    protected static void BasicMovement(Rigidbody2D target, Vector3 direction, float speed)
    {
        var animator = target.GetComponentInChildren <Animator>();

        if (animator != null)
        {
            animator.SetBool("is_moving", direction.magnitude > 0);
            if (direction != Vector3.zero)
            {
                animator.SetFloat("input_x", direction.x);
                animator.SetFloat("input_y", direction.y);
            }
        }

        target.velocity = Vector3.ClampMagnitude(direction, 1f) * speed;
        if (Mathf.Abs(direction.x) > 0)
        {
            target.transform.localScale = direction.x < 0f ? new Vector3(1f, 1f, 1f) : new Vector3(-1f, 1f, 1f);
        }
    }
コード例 #11
0
    private Vector2 SmoothSeek(Rigidbody2D target, float range = -1, float playerPower = 1, float offset = 0)
    {
        if (range == -1)
        {
            range = m_SeekRange;
        }

        Vector2 cible = target.position;

        if (offset != 0)
        {
            BergerBehavior Be       = target.GetComponentInChildren <BergerBehavior>();
            Vector2        decalage = new Vector2(Be.transform.right.x * -offset, Be.transform.right.y * -offset);
            cible = cible + decalage;
        }



        return(-GetRepulsePowerFrom(cible, range, m_SeekPower) * playerPower);
    }
コード例 #12
0
    // startAngle of 0 makes the first particle move to the right,
    // with 360 degrees being a full circle.
    // Usually a maxAngle of 360 and any startAngle will suffice.
    // shotCount is the number of explosions that will be equally
    // spread between the start and max angles.
    // speed is the speed of each shot.
    private void CreateRoundExplosion(float startAngle, float maxAngle, int shotCount, float speed)
    {
        // Finds the starting direction and the angle increment per shot.
        Vector2 direction    = Quaternion.AngleAxis(startAngle, Vector3.forward) * Vector2.right;
        float   anglePerShot = maxAngle / shotCount;

        // Creates each shot and increases the angle by the desired angle step.
        for (int i = 0; i < shotCount; i++)
        {
            Rigidbody2D b = Instantiate(explosionParticle.gameObject).GetComponent <Rigidbody2D>();
            b.velocity = direction * speed;

            b.transform.parent   = transform;
            b.transform.position = transform.position;

            b.GetComponentInChildren <Animator>().Play("Looped");

            direction = Quaternion.AngleAxis(anglePerShot, Vector3.forward) * direction;
        }
    }
コード例 #13
0
    private void FixedUpdate()
    {
        if (transform.position.y - CurrentHeight >= Distance)
        {
            CatJump = false;
        }

        if (CatJump && Input.GetButton("Fire1"))
        {
            BlackCat.AddForce(new Vector2(ForceX, ForceY));
            Anim.SetBool("Grounded", false);
        }

        if (isDead)
        {
            CircleCollider2D cir = BlackCat.GetComponent <CircleCollider2D>();
            BoxCollider2D    box = BlackCat.GetComponentInChildren <BoxCollider2D>();
            cir.enabled = false;
            box.enabled = false;
            BlackCat.AddForce(new Vector2(-15.0f * Time.deltaTime, 15.0f * Time.deltaTime));
        }
    }
コード例 #14
0
    private IEnumerator EnterHole(Rigidbody2D rb)
    {
        Vector2 direction;
        float   t = 0f, timer = 0.5f;

        rb.velocity = ((Vector2)transform.position - rb.position).normalized * 10f;
        float initialDrag = rb.drag;

        rb.drag = 10f;
        Vector3        scale = rb.transform.localScale, targetScale = rb.transform.localScale * 0.2f;
        SpriteRenderer r     = rb.GetComponentInChildren <SpriteRenderer>();
        Color          color = r.color;

        while (t < timer)
        {
            if (rb)
            {
                rb.transform.localScale = Vector3.Slerp(scale, targetScale, t / timer);
                r.color     = Color.Lerp(color, Color.black, t / timer);
                direction   = ((Vector2)transform.position - rb.position).normalized;
                rb.velocity = direction * rb.velocity.magnitude * (1 - 2f * Time.deltaTime);
                Debug.DrawRay(rb.position, rb.velocity, Color.cyan);
            }
            t += Time.deltaTime;
            yield return(new WaitForEndOfFrame());
        }
        if (rb)
        {
            rb.drag = initialDrag;
            rb.transform.localScale = scale;
            r.color = color;

            if (rb.gameObject.HasTag(Tag.Cue))
            {
                BuildController.instance.hold(rb.gameObject);
            }
            Destroy(rb.gameObject);
        }
    }
コード例 #15
0
    public void LaunchBall()
    {
        if (HoldingBall)
        {
            _heldBall.simulated   = true;
            _heldBall.isKinematic = false;

            _heldBall.transform.SetParent(null);

            Ball ball = _heldBall.GetComponentInParent <Ball>();

            var paddle = GetComponentInParent <PaddleController>();

            ball.ChangeSpeedAndSetDirection(_launchStrength + paddle.SpeedIncreasePerBounce, GetOffsetLaunchDirection());
            _heldBall.GetComponentInChildren <TrailRenderer>().emitting = true;

            _heldBall = null;

            _launchAngleOffset = 0;
            _timeHeld          = 0;
            _launchDirectionVisualizer.localRotation = Quaternion.identity;
        }
    }
コード例 #16
0
    public void CatchBall(Ball ball)
    {
        if (ball == null)
        {
            return;
        }

        _heldBall = ball.GetComponentInParent <Rigidbody2D>();

        if (HoldingBall)
        {
            _heldBall.GetComponentInChildren <TrailRenderer>().emitting = false;
            ball.ResetSpeed();
            _heldBall.simulated   = false;
            _heldBall.isKinematic = true;

            _heldBall.transform.position = _holdPosition.position;
            _heldBall.transform.SetParent(_holdPosition);
            var paddle = GetComponentInParent <PaddleController>();
            _scoreKeeper.CaughtBall(paddle);

            ball.HitByPaddle(paddle);
        }
    }
コード例 #17
0
    public void ForcePulseExplosion()
    {
        Instantiate(forcePulseWave, transform.position, Quaternion.identity, ParticleGenerator.holder);
        Vector2 point  = transform.position;
        int     layers = (1 << LayerSolid) | (1 << LayerProjectile);

        Collider2D[]       colliders = Physics2D.OverlapCircleAll(point, explosionRadius, layers);
        List <Rigidbody2D> rbs       = new List <Rigidbody2D>();

        for (int i = 0; i < colliders.Length; i++)
        {
            Collider2D col = colliders[i];
            if (col.attachedRigidbody.bodyType == RigidbodyType2D.Static)
            {
                continue;
            }
            bool found = false;
            for (int j = 0; j < rbs.Count; j++)
            {
                Rigidbody2D colRb = rbs[j];
                if (colRb == col.attachedRigidbody)
                {
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                rbs.Add(col.attachedRigidbody);
            }
        }

        for (int i = 0; i < rbs.Count; i++)
        {
            Rigidbody2D colRb     = rbs[i];
            IStunnable  stunnable = colRb.GetComponent <IStunnable>();
            stunnable = stunnable ?? colRb.GetComponentInChildren <IStunnable>();
            stunnable?.Stun();

            if (colRb == rb)
            {
                continue;
            }
            Vector2 dir      = ((Vector2)colRb.transform.position - point).normalized;
            float   distance = Vector2.Distance(point, colRb.transform.position);
            if (distance >= explosionRadius)
            {
                continue;
            }
            colRb.velocity += dir * Mathf.Pow((explosionRadius - distance) / explosionRadius, 0.5f)
                              * explosionStrength;
            colRb.AddTorque((UnityEngine.Random.value > 0.5 ? 1f : -1f) * explosionStrength * 5f);
        }

        Vector2 screenPos = Camera.main.WorldToViewportPoint(transform.position);

        if (screenPos.x > -0.5f || screenPos.x < 1.5f || screenPos.y > -0.5f || screenPos.y < 1.5f)
        {
            screenRippleSO.StartRipple(this, distortionLevel: 0.03f,
                                       position: screenPos);
        }
    }
コード例 #18
0
        private float GetSurfaceRadius()
        {
            CircleCollider2D orbitCollider = m_orbitTarget.GetComponentInChildren <CircleCollider2D>();

            return(orbitCollider.radius);
        }
コード例 #19
0
    // Update is called once per frame
    void Update()
    {
        //TODO:在地面上才能跳
        //if (m_JellySp != null)
        //{
        //    m_JellySp.AddForce(Strenth * new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")));
        //    if (Input.GetKeyDown(KeyCode.Space) | Input.GetKeyDown(KeyCode.JoystickButton0)) m_JellySp.AddForce(new Vector2(0, JumpForce));
        //}
        //else
        //{
        //    if (Input.GetKeyDown(KeyCode.Space) | Input.GetKeyDown(KeyCode.JoystickButton0)) GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpForce));
        //}

        //if (transform.position.x < Controller.minX || transform.position.y > Controller.maxX)
        //    StartCoroutine(m_Controller.Lose());

        if (Input.GetKeyDown(KeyCode.Q))
        {
            //mLeftArm.transform.eulerAngles = new Vector3(0, 0, 0f);
            mLeftArm.AddRelativeForce(Strenth * new Vector2(0, 1), ForceMode2D.Impulse);
            mLeftArm.GetComponentInChildren <SpriteRenderer>().transform.DOKill();
            mLeftArm.GetComponentInChildren <SpriteRenderer>().transform.localScale = new Vector3(1, 2.5f, 1);
            mLeftArm.GetComponentInChildren <SpriteRenderer>().transform.DOScaleY(1, 0.5f);
        }
        if (Input.GetKeyDown(KeyCode.W))
        {
            mRightArm.AddRelativeForce(Strenth * new Vector2(0, 1), ForceMode2D.Impulse);
            mRightArm.GetComponentInChildren <SpriteRenderer>().transform.DOKill();
            mRightArm.GetComponentInChildren <SpriteRenderer>().transform.localScale = new Vector3(1, 2.5f, 1);
            mRightArm.GetComponentInChildren <SpriteRenderer>().transform.DOScaleY(1, 0.5f);
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            mLeftLeg.AddRelativeForce(Strenth * new Vector2(0, -1), ForceMode2D.Impulse);
            mLeftLeg.GetComponentInChildren <SpriteRenderer>().transform.DOKill();
            mLeftLeg.GetComponentInChildren <SpriteRenderer>().transform.localScale = new Vector3(1, 2.5f, 1);
            mLeftLeg.GetComponentInChildren <SpriteRenderer>().transform.DOScaleY(1, 0.5f);
        }
        if (Input.GetKeyDown(KeyCode.R))
        {
            mRightLeg.AddRelativeForce(Strenth * new Vector2(0, -1), ForceMode2D.Impulse);
            mRightLeg.GetComponentInChildren <SpriteRenderer>().transform.DOKill();
            mRightLeg.GetComponentInChildren <SpriteRenderer>().transform.localScale = new Vector3(1, 2.5f, 1);
            mRightLeg.GetComponentInChildren <SpriteRenderer>().transform.DOScaleY(1, 0.5f);
        }
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            if (GetComponent <Rigidbody2D>().bodyType != RigidbodyType2D.Kinematic)
            {
                GetComponent <Rigidbody2D>().velocity = Vector2.zero;
                GetComponent <Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
                HP = 5000;
            }
            else
            {
                GetComponent <Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
                HP = InitalHP;
            }
        }
        if (HP < 0)
        {
            StartCoroutine(m_Controller.Lose("GAME OVER : OVERCROWDED"));
        }
        HPBar.size = Mathf.Clamp01(HP / InitalHP);

        HPBar.transform.position = Camera.main.WorldToScreenPoint(transform.position + new Vector3(0, 14f, 0));
    }
コード例 #20
0
    // Update is called once per frame
    void Update()
    {
        bodyinfo.GetComponentInChildren <Animator>().SetBool("Deactivated", !possessed);
        bodyinfo.GetComponentInChildren <Animator>().SetBool("Jumping", jumping);
        bodyinfo.GetComponentInChildren <Animator>().SetBool("Falling", falling);

        if (freezeTimer > 0)
        {
            freezeTimer -= Time.deltaTime;
            return;
        }

        //Center on dat bot! and LERP!
        gameObject.transform.position = Vector2.Lerp(gameObject.transform.position, target.gameObject.transform.position, Time.deltaTime * 4);

        //Get dat inputs!
        float hAxis   = Input.GetAxis("Horizontal");
        float vAxis   = Input.GetAxis("Vertical");
        bool  jumpBtn = Input.GetButtonDown("Jump");


        if (possessed) //If you are controlling a bot...
        {
            //Keep the soul centered
            Vector2 temp = new Vector2(transform.position.x, transform.position.y + bodyinfo.m_soulOffset_y);
            soul.transform.position = temp;

            Vector2 appliedForce = Vector2.zero;

            if (jumpTimer > 0)
            {
                jumpTimer -= Time.deltaTime;
            }
            Vector2 sidetest1 = transform.position;
            sidetest1.x -= 0.35f;
            Vector2 sidetest2 = transform.position;
            sidetest2.x += 0.35f;
            if (Physics2D.Raycast(gameObject.transform.position, new Vector2(0, -1f), 0.05f) || Physics2D.Raycast(sidetest2, new Vector2(0, -1f), 0.05f) || Physics2D.Raycast(sidetest1, new Vector2(0, -1f), 0.05f))
            {
                if (falling || jumping)
                {
                    falling = false;
                    jumping = false;
                }
                if (Input.GetButtonDown("Transmit"))
                {
                    possessed = false;
                    soul.SetActive(true);
                    jumpQueued = false;
                    jumpTimer  = 0.0f;
                }
                if (!jumpQueued)
                {
                    appliedForce = new Vector2(hAxis * 10.0f, 0.0f);
                }
                if (jumpBtn)
                {
                    jumpQueued = true;
                    jumpTimer  = bodyinfo.m_jumpDelay;
                    jumping    = true;
                }
            }
            else
            {
                appliedForce = new Vector2(hAxis * 5.0f, 0.0f);
            }
            if (jumpTimer <= 0.0f && jumpQueued)
            {
                jumpQueued     = false;
                appliedForce.y = 5.0f * bodyinfo.m_jumpStrength / Time.fixedDeltaTime;
            }

            target.AddForce(appliedForce);
            {
                Vector2 t = new Vector2(target.velocity.x, 0);
                target.GetComponentInChildren <Animator>().SetBool("Walking", t.magnitude > 0);
                if (t.x > 0)
                {
                    target.GetComponentInChildren <SpriteRenderer>().flipX = false;
                }
                if (t.x < 0)
                {
                    target.GetComponentInChildren <SpriteRenderer>().flipX = true;
                }
                if (t.magnitude > bodyinfo.m_maxSpeed)
                {
                    t.Normalize();
                    t              *= bodyinfo.m_maxSpeed;
                    t.y             = target.velocity.y;
                    target.velocity = t;
                }
            }
            if (jumping && target.velocity.y < 0)
            {
                jumping = false;
                falling = true;
            }
        }
        else
        {
            Vector3 centre = new Vector2(transform.position.x, transform.position.y + bodyinfo.m_soulOffset_y);

            if (Input.GetButtonDown("Cancel"))
            {
                possessed = true;
                soul.SetActive(false);
            }

            if (Input.GetButtonDown("Transmit"))
            {
                RaycastHit2D hit;
                if (hit = Physics2D.Raycast(soul.transform.position, new Vector2(0, 0), 0))
                {
                    if (hit.collider.gameObject.tag == "Player")
                    {
                        target    = hit.collider.gameObject.GetComponent <Rigidbody2D>();
                        bodyinfo  = hit.collider.gameObject.GetComponent <PlayerBody>();
                        possessed = true;
                        soul.SetActive(false);
                        freezeTimer = bodyinfo.m_possessDelay;
                    }
                }
            }
            Vector2 vel = new Vector2(hAxis, vAxis);
            vel *= Time.fixedDeltaTime * 5.0f;
            soul.transform.Translate(vel);

            if ((soul.transform.position - centre).magnitude > 3.5f)
            {
                Vector2 dir = soul.transform.position - centre;
                dir.Normalize();
                dir *= 3.5f;
                Vector2 temp = new Vector2(transform.position.x + dir.x, transform.position.y + dir.y + bodyinfo.m_soulOffset_y);
                soul.transform.position = temp;
            }
        }
    }
コード例 #21
0
 void Start()
 {
     player = GameObject.FindGameObjectWithTag("Player").GetComponent <Rigidbody2D>();
     playerGroundedCheck = player.GetComponentInChildren <PlayerGroundedCheck>();
 }