public bool IsGrounded()
    {
        if (_rigidbody.IsTouchingLayers(WallMask))
        {
            _rigidbody.GetContacts(_filter, _contacts);
            if (_contacts.Any(c => Vector2.Dot(c.normal, Vector2.up) == 1))
            {
                return true;
            }
        }

        return false;
    }
Beispiel #2
0
    void FixedUpdate()
    {
        int numberOfContacts = rb2d.GetContacts(new ContactPoint2D[1]);

        if (numberOfContacts > 0)
        {
            Vector2 normal = GetContactNormal();
            if (normal == lastContactNormal)
            {
                return;
            }
            lastContactNormal = normal;
            ball.state.contactNormal.Value = normal;
        }
    }
    // Update is called once per frame
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        bool  onGround        = (playerRigidBody.GetContacts(new List <Collider2D>()) > 0); // Checks if the player is colliding with ANYTHING

        playerAnimator.SetFloat("MoveSpeed", Mathf.Abs(horizontalInput));
        playerAnimator.SetFloat("VerticalSpeed", playerRigidBody.velocity.y);

        if (horizontalInput != 0)
        {
            playerRigidBody.velocity = new Vector2(horizontalInput * moveSpeed, playerRigidBody.velocity.y);

            if (horizontalInput > 0)
            {
                playerSpriteRenderer.flipX = false;
            }
            else
            {
                playerSpriteRenderer.flipX = true;
            }
        }
        if (Input.GetButtonDown("Jump") && onGround)
        {
            playerRigidBody.AddForce(Vector2.up * jumpStrength, ForceMode2D.Impulse);
        }
    }
Beispiel #4
0
    private void HandleGroundState()
    {
        // grounded
        prevGrounded = grounded;

        List <ContactPoint2D> contacts = new List <ContactPoint2D>();
        int totalContacts = _rb.GetContacts(contacts);

        Vector2 netNormal = Vector2.zero;

        for (int i = 0; i < totalContacts; i++)
        {
            netNormal += contacts[i].normal;
        }

        netNormal /= totalContacts;
        netNormal  = netNormal.normalized;

        grounded = netNormal.y > minContactNormalY ? true : false;

        if (grounded != prevGrounded)
        {
            if (grounded)
            {
                GroundedStart();
            }
            else
            {
                GroundedExit();
            }
        }
    }
Beispiel #5
0
    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Brick")
        {
            m_levelEntities.RemoveBrick(col.gameObject);
        }
        else if (col.gameObject.tag == "Paddle")
        {
            GameObject  paddle = col.gameObject;
            Rigidbody2D rb     = GetComponent <Rigidbody2D>();

            rb.GetContacts(m_contacts);
            m_direction.Set(m_contacts[0].normal.x, m_contacts[0].normal.y);
            m_directionChanged = true;
            m_levelEntities.SoundBallDeflect();
        }
        else if (col.gameObject.name == "wall_bottom")
        {
            m_levelEntities.RemoveBall(gameObject);
        }
        else
        {
            m_levelEntities.SoundBallDeflect();
        }
    }
Beispiel #6
0
    private void Update()
    {
        slide = (rb2d.GetContacts(cf2d, contacts) <= 0);

        if (Input.GetKey(KeyCode.LeftControl))
        {
            crouch = true;

            //GetComponent<BoxCollider2D>().offset = new Vector2(0, -0.25f);
            //GetComponent<BoxCollider2D>().size = new Vector2(0.6f, 0.6f);
        }
        else
        {
            crouch = false;

            GetComponent <BoxCollider2D>().size = new Vector2(0.6f, 0.6f);
        }

        anim.SetBool("crouch", crouch);

        if (Input.GetMouseButtonDown(1))
        {
            golpePurificante = true;
        }
        else
        {
            golpePurificante = false;
        }

        anim.SetFloat("velocityX", rb2d.velocity.x);
        anim.SetBool("golpePurificante", golpePurificante);
    }
Beispiel #7
0
        private void FindContacts()
        {
            _groundContact  = null;
            _ceilingContact = null;
            _wallContact    = null;

            float groundProjection  = maxWalkCos;
            float wallProjection    = maxWalkCos;
            float ceilingProjection = -maxWalkCos;

            int numberOfContacts = _rigidbody2D.GetContacts(_contactFilter, _contacts);

            for (var i = 0; i < numberOfContacts; i++)
            {
                var   contact    = _contacts[i];
                float projection = Vector2.Dot(Vector2.up, contact.normal);

                if (projection > groundProjection)
                {
                    _groundContact   = contact;
                    groundProjection = projection;
                }
                else if (projection < ceilingProjection)
                {
                    _ceilingContact   = contact;
                    ceilingProjection = projection;
                }
                else if (projection <= wallProjection)
                {
                    _wallContact   = contact;
                    wallProjection = projection;
                }
            }
        }
Beispiel #8
0
    void setSpeed(float newSpeed)
    {
        ContactPoint2D[] contacts = new ContactPoint2D[10];
        int num = rb.GetContacts(contacts);

        for (int i = 0; i < num; i++)
        {
            if (Math.Abs(contacts[i].normal.x) > 0 &&
                (rb.position.y - 1.3 < contacts[i].point.y) &&
                ((rb.position.x + 0.25 < contacts[i].point.x &&
                  isFacingRight) ||
                 (rb.position.x - 0.25 > contacts[i].point.x &&
                  !isFacingRight)))
            {
                speed = 0;
                return;
            }
        }
        if (Math.Abs(newSpeed) < maxSpeed)
        {
            speed = newSpeed;
        }
        else
        {
            speed = maxSpeed * ((newSpeed > 0) ? 1 : -1);
        }
    }
Beispiel #9
0
    void checkGrounded()
    {
        bool isIn = false;

        Collider2D[] inContact = new Collider2D[10];
        r_body.GetContacts(inContact);
        for (int i = 0; i < inContact.Length; i++)
        {
            if (null == inContact[i])
            {
            }
            else
            {
                if (inContact[i].gameObject.tag == "platforms" || inContact[i].gameObject.tag == "enemy")
                {
                    isIn = true;
                }
            }
        }
        if (isIn)
        {
            grounded = true;
        }
        else
        {
            grounded = false;
        }
    }
Beispiel #10
0
        private void Jump()
        {
            // only jump if moving down
            if (rb2d.velocity.y > 0f)
            {
                return;
            }

            // get contacts
            List <ContactPoint2D> contactPoints = new List <ContactPoint2D>();

            rb2d.GetContacts(contactPoints);

            Vector2 myPos = transform.position;

            // check normals of contacts, discarding others
            foreach (ContactPoint2D contactPoint in contactPoints)
            {
                float rot = Vector2.Angle(Vector2.up, contactPoint.normal);
                if (rot >= -15f && rot <= 15f)
                {
                    // add jump force
                    rb2d.AddForce(jumpForce * Vector2.up, ForceMode2D.Impulse);
                    onJump.Invoke();
                    return;
                }
            }
        }
Beispiel #11
0
    private void Update()
    {
        Vector3 targetPos = transform.position;

        targetPos.x        = startX;
        transform.position = targetPos;

        //rb.rotation = Mathf.Clamp(rb.rotation, -maxTilt, maxTilt);

        float averageX = 0f;
        List <ContactPoint2D> contactPoints = new List <ContactPoint2D>();
        int count = rb.GetContacts(contactPoints);

        if (count == 0)
        {
            rb.centerOfMass = startCenterMass;
            return;
        }

        foreach (ContactPoint2D contactPoint in contactPoints)
        {
            averageX += contactPoint.otherCollider.gameObject.transform.localPosition.x;
        }
        averageX /= -count;

        rb.centerOfMass = new Vector2(averageX, rb.centerOfMass.y);
    }
    private void FixedUpdate()
    {
        rb.GetContacts(_contactPoints);

        var contactPoint2D = _contactPoints.FirstOrDefault(contactPoint => _colliders.Contains(contactPoint.collider));

        if (contactPoint2D.normal.x == 0 &&
            contactPoint2D.normal.y > 0 &&
            contactPoint2D.enabled &&
            Mathf.Abs(rb.velocity.y) < 0.02f)
        {
            var posY   = contactPoint2D.collider.transform.position.y;
            var scaleY = contactPoint2D.collider.transform.lossyScale.y;

            var pointExpected = posY + scaleY / 2;

            var diff = pointExpected - contactPoint2D.point.y;

            if (Mathf.Abs(diff) > 0)
            {
                rb.velocity = new Vector3(
                    rb.velocity.x,
                    0
                    );

                transform.position = new Vector2(
                    transform.position.x,
                    transform.position.y + diff
                    );
            }
        }
    }
Beispiel #13
0
    void Update()
    {
        if (SceneManager.sceneCount != 1)
        {
            return;
        }
        //get inputs
        x = Input.GetAxisRaw("Horizontal");

        //Eliminate rotation inertia when touching something but not moving
        rb.constraints = (x == 0 && rb.GetContacts(new ContactPoint2D[10]) != 0) ? RigidbodyConstraints2D.FreezeRotation : RigidbodyConstraints2D.None;

        if (Input.GetKeyDown(KeyCode.Space) && CanJump())
        {
            Jump();
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (SceneManager.GetActiveScene().name.Equals("Level_01"))
            {
                GameObject.Find("PlayerCharacter").GetComponent <NarrateL1>().ShowMessage("Don't bother with the settings. They're broken.");
            }
            else if (SceneManager.GetActiveScene().name.Equals("Maze"))
            {
                GameObject.Find("PlayerCharacter").GetComponent <NarrateL2>().ShowMessage("Don't bother with the settings. They're broken.");
            }
            SceneManager.LoadScene("Menu", LoadSceneMode.Additive);
        }

        //use the sounds slider in the audio options to change the volume of the jump.
        audioSource.volume = Settings.GetFloat("Sounds");
    }
    // obj follow paltform
    public void FollowObjects()
    {
        int count = rb.GetContacts(contactFilter, contactPoint);

        for (int i = 0; i < count; i++)
        {
            contactPoint[i].rigidbody.velocity += new Vector2(isMoveToEnd?speed:-speed, 0);
        }
    }
    public void ProcessMouseDown()
    {
        ContactPoint2D[] contactPoints = new ContactPoint2D[10];
        int contacts = rd2D.GetContacts(contactPoints);

        for (int i = 0; i < contacts; i++)
        {
            GameObject obj = contactPoints[i].collider.gameObject;
            DamageObject(obj);
        }
    }
Beispiel #16
0
        public static List <ContactPoint2D> GetContacts(this Rigidbody2D @this, ContactFilter2D filter)
        {
            var result     = new List <ContactPoint2D>();
            var collisions = new ContactPoint2D[32];
            var count      = @this.GetContacts(filter, collisions);

            for (var i = 0; i < count; i++)
            {
                result.Add(collisions[i]);
            }
            return(result);
        }
Beispiel #17
0
    void FixedUpdate()
    {
        if (!autoMove)
        {
            return;
        }

        if (!shooting || moveWhileShooting)
        {
            float   radiusError = relativeAimPos.magnitude - preferredOrbitalRadius;
            Vector2 playerward  = relativeAimPos.normalized;

            Vector2 radialComponent     = playerward * radiusError * (hasLineOfSight ? 1 : 0.5f);
            Vector2 tangentialComponent = Vector2.Perpendicular(playerward) * orbiticity;
            Vector2 direction           = (radialComponent + tangentialComponent).normalized;

            // I plead guilty to all charges of awful control flow
            List <ContactPoint2D> contacts = new List <ContactPoint2D>();
            rb.GetContacts(contacts);
            foreach (ContactPoint2D contact in contacts)
            {
                if (Vector3.Angle(-contact.normal, tangentialComponent) < 60)                 // If almost perpendicular strike (of sidewalking component),
                {
                    orbiticity *= -1;                                                         // switch direction.
                }
                else if (Vector3.Angle(-contact.normal, direction) < 90)                      // otherwise, if we really are running into the wall,
                {
                    Vector2 newAxis = Vector2.Perpendicular(contact.normal);                  // redirect to run along contact tangent.
                    direction = newAxis * (Vector2.Angle(newAxis, tangentialComponent) > 90 ? -1 : 1);
                }
            }
            motivity.Motivate(rb, direction, room.timeScale);
        }

        if (!shooting || turnWhileShooting)
        {
            float targetAngle = Vector2.SignedAngle(Vector2.up, relativeAimPos);
            if (redirecting)
            {
                float currentAngle = Vector2.SignedAngle(Vector2.up, transform.up);
                float newAngle     = Mathf.MoveTowardsAngle(currentAngle, targetAngle, room.deltaTime * 270);
                transform.rotation = Quaternion.Euler(0, 0, newAngle);
                if (Mathf.Abs(newAngle - targetAngle) < 1)
                {
                    redirecting = false;
                }
            }
            else
            {
                transform.rotation = Quaternion.Euler(0, 0, targetAngle);
            }
        }
    }
    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;
            }
        }
    }
Beispiel #19
0
    void FixedUpdate()
    {
        foreach (CatcherNode node in nodes)
        {
            node.isConnect = false;
        }

        int contactCount = body.GetContacts(contactFilter, contactPoints);

        for (int i = 0; i < contactCount; i++)
        {
            ContactPoint2D contact = contactPoints[i];

            //Contact with self
            if (contact.rigidbody == body)
            {
                continue;
            }

            float dot = Vector2.Dot(contact.normal, Vector2.down);
            //No top contact
            if (dot < 0.8f)
            {
                continue;
            }

            Rigidbody2D rigi = contact.rigidbody;
            //Contact not with rigidbody
            if (rigi == null)
            {
                continue;
            }

            CatcherNode node = nodes.Find((n) => n.rigi == rigi);
            if (node == null)
            {
                node = new CatcherNode()
                {
                    rigi = rigi
                };
                nodes.Add(node);
                Debug.Log(contact.collider.name);
            }

            node.isConnect = true;
        }

        foreach (CatcherNode node in nodes)
        {
            node.Reparent(transform);
        }
    }
Beispiel #20
0
    public void FollowObjects()
    {
        if (!triggerMove)
        {
            return;
        }
        int count = rb.GetContacts(cf, cp);

        for (int i = 0; i < count; i++)
        {
            cp[i].rigidbody.velocity += new Vector2(isMoveToEnd?-speed:speed, 0);
        }
    }
    // Update is called once per frame
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");

        if (horizontalInput != 0)
        {
            transform.Translate(Vector2.right * horizontalInput * moveSpeed * Time.deltaTime);
        }
        if (Input.GetKeyDown(KeyCode.Space) && (playerRB.GetContacts(new BoxCollider2D[] { playerBC }) > 0)) // Checks to see if the player is colliding with ANYTHING
        {
            playerRB.AddForce(Vector2.up * jumpStrength, ForceMode2D.Impulse);
        }
    }
Beispiel #22
0
 private void FixedUpdate()
 {
     OnGround = false;
     rig.GetContacts(colliders);
     foreach (Collider2D collider in colliders)
     {
         if (collider.CompareTag("Track"))
         {
             OnGround = true;
             break;
         }
     }
 }
    /**Checks if player is hitting something in a certain direction
     *     Vector2 normal - the desired direction of contact
     */
    GameObject isContact(Vector2 normal)
    {
        int count = rb.GetContacts(points); //get all the contact points.

        for (int i = 0; i < count; i++)     //iterate through contact points
        {
            if (Vector2.Dot(points[i].normal, normal) > 0.9)
            { //if the contact normal 90% in the same direction as the desired direction
                return(points[i].collider.gameObject);
            }
        }
        return(null);
    }
Beispiel #24
0
        private void CheckRigidbodyContacts(Rigidbody2D rb)
        {
            var contactCount = rb.GetContacts(contactFilter, m_ContactPoints);

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

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

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

                                m_CaughtObjects.Add(newCaughtObject);
                            }
                        }
                    }
                }
                else
                {
                    m_CaughtObjects[listIndex].inContact = true;
                }
            }
        }
        // Update is called once per frame
        private void Update()
        {
            var horizontal = Input.GetAxis(horizontalAxis);

            if (.1f < Mathf.Abs(horizontal))
            {
                transform.position += new Vector3(Mathf.Sign(horizontal) * speed * Time.deltaTime, 0);
            }

            if (.1f < Input.GetAxis(jumpAxis) && 0 < rigidBody.GetContacts(contacts))
            {
                rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpForce);
            }
        }
    // Update is called once per frame
    void Update()
    {
        //rigi.AddForce(Vector2.right * moveForce * Input.GetAxisRaw("Horizontal") * Time.deltaTime);
        rigi.AddForceAtPosition(transform.right * moveForce * Input.GetAxisRaw("Horizontal") * Time.deltaTime, transform.position);

        if (Input.GetKeyDown(KeyCode.O))
        {
            int numeContacts = rigi.GetContacts(contacts);
            for (int i = 0; i < numeContacts; i++)
            {
                ContactPoint2D contact = contacts[i];
                Instantiate(eyePref, contact.point, Quaternion.identity);
            }
        }
    }
Beispiel #27
0
    public MovementState FixedUpdate()
    {
        var contactPoints = new List <ContactPoint2D>();

        rigidbody.GetContacts(contactPoints);

        if (contactPoints.Count > 0)
        {
            return(MovementState.Sliding);
        }

        transform.up = rigidbody.velocity;

        return(MovementState.Flying);
    }
Beispiel #28
0
    private bool IsOnGround()
    {
        ContactPoint2D[] contacts = new ContactPoint2D[8];
        m_body.GetContacts(contacts);

        foreach (ContactPoint2D contact in contacts)
        {
            if (contact.normal.y > 0.6)
            {
                return(true);
            }
        }

        return(false);
    }
Beispiel #29
0
        private void CheckRigidbodyContacts(Rigidbody2D _rigidbody)
        {
            int contactCount = _rigidbody.GetContacts(contact_filter, m_contact_points);

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

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

                if (listIndex == -1)
                {
                    if (contactRigidbody != null)
                    {
                        if (contactRigidbody.bodyType != RigidbodyType2D.Static && contactRigidbody != platform_rigidbody)
                        {
                            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>(),
                                    in_contact         = true,
                                    checked_this_frame = false
                                };

                                m_caught_objects.Add(newCaughtObject);
                            }
                        }
                    }
                }
                else
                {
                    m_caught_objects[listIndex].in_contact = true;
                }
            }
        }
    public bool isNormalForceInDirection(int direction)          // 1 is up, 2 is right, 3 is down, 4 is left
    {
        ContactPoint2D[] contactPoints = new ContactPoint2D[20]; // this 20 is an intentional over estimate
        int numOfContacts = rb.GetContacts(contactPoints);

        if (direction == 1)
        {
            for (int i = 0; i < numOfContacts; i++)
            {
                if (contactPoints[i].normal.y >= 0.7f) // if on any contact points the y axis is experienceing an upwards normal force, ie it is on top of an object
                {
                    return(true);
                }
            }
        }
        else if (direction == 2)
        {
            for (int i = 0; i < numOfContacts; i++)
            {
                if (contactPoints[i].normal.x >= 0.7f)
                {
                    return(true);
                }
            }
        }
        else if (direction == 3)
        {
            for (int i = 0; i < numOfContacts; i++)
            {
                if (contactPoints[i].normal.y <= -0.7f)
                {
                    return(true);
                }
            }
        }
        else if (direction == 4)
        {
            for (int i = 0; i < numOfContacts; i++)
            {
                if (contactPoints[i].normal.x <= -0.7f)
                {
                    return(true);
                }
            }
        }
        return(false);
    }