Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FireNinja101 · May 14, 2019 at 08:25 AM · collision2d-platformer

Unity 2D Sprite colliders not registering

I have two sprites in Unity, neither of which are registering collisions. Both objects have non-kinematic rigidbodies attached and they both have normal colliders (not trigger colliders). Neither of them are registering collisions with other objects. I also have tried using Physics2D.OverlapArea to try and manually set up collisions, but that isn't working either. Any ideas? Here is the code attached to the enemy object:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyScript : MonoBehaviour {
 
     public float duration;
     private Rigidbody myRB;
     public float moveSpeed;
     public Transform myPlayer;
     public LayerMask playerLayer;
     public Transform TL;
     public Transform BR;
 
     // Use this for initialization
     void Start () {
         myRB = gameObject.GetComponent<Rigidbody>();
     }
     
     // Update is called once per frame
     void Update () {
 
         Debug.Log(Physics2D.OverlapArea(TL.position, BR.position, playerLayer, -300, 300));
 
         duration -= Time.deltaTime;
         if (duration <= 0f)
         {
             Destroy(gameObject, 0f);
         }
 
         myRB.velocity = new Vector3(-moveSpeed * Time.deltaTime, 0, 0);s
     }
 }


Here is the code attached to the main player:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerMovement : MonoBehaviour {
 
     public float walkSpeed;
     private Vector3 curMovement;
 
     private SpriteRenderer mySR;
     public bool isFlipped;
 
     public float maxHealth;
     private float curHealth;
 
     public float myDmg;
     public float curArmor;
     public float maxArmor;
 
     private float curTime;
     public float boostMultiplier;
     public float boostCooldown;
 
     private Rigidbody myRB;
 
     public Transform bulletSpawnRight;
     public Transform bulletSpawnLeft;
     public GameObject bulletPrefab;
     public float fireDelay;
     private float fireDelayReset;
     private bool hasFired;
 
     public float leftBorder;
     public float rightBorder;
     public float topBorder;
     public float bottomBorder;
 
     public GameObject EngineLeft;
     public GameObject EngineRight;
 
     public float En1Dmg;
     public float En2Dmg;
     public float En3Dmg;
     public float Boss1Dmg;
     public float Boss2Dmg;
 
     public Transform HitBoxTLFlipped;
     public Transform HitBoxBRFlipped;
     public Transform HitBoxTL;
     public Transform HitBoxBR;
     public LayerMask Enemy1;
     public LayerMask Enemy2;
     public LayerMask Enemy3;
     public LayerMask Boss1;
     public LayerMask Boss2;
     private Collider2D enemyCollider;
     private bool isTouched;
 
     // Use this for initialization
     void Start () {
         fireDelayReset = fireDelay;
         myRB = gameObject.GetComponent<Rigidbody>();
         curMovement = new Vector3(walkSpeed * Time.deltaTime, walkSpeed * Time.deltaTime, 0);
         mySR = gameObject.GetComponent<SpriteRenderer>();
         if (mySR.flipX == true)
         {
             isFlipped = true;
         } else
         {
             isFlipped = false;
         }
     }
 
     // Update is called once per frame
     void Update() {
         RotationReset();
         EngineRotation();
         Movement();
         BorderChecker();
         FireGun();
         PlayerFacing();
         FireChecker();
         HitBoxChecker();
     }
 
     void RotationReset()
     {
         transform.localRotation = new Quaternion(0, 0, 0, 0);
     }
 
     void EngineRotation()
     {
         if (isFlipped == true)
         {
             EngineRight.gameObject.SetActive(true);
             EngineLeft.gameObject.SetActive(false);
         } if (isFlipped == false)
         {
             EngineRight.gameObject.SetActive(false);
             EngineLeft.gameObject.SetActive(true);
         }
     }
 
     void Movement()
     {
 
         //Handle the player movement
         //subtract from the x to move left, add to move right
         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
         {
             if (Input.GetKey(KeyCode.A))
             {
                 curMovement.x = -walkSpeed;
                 myRB.velocity = curMovement;
             }
             else if (Input.GetKey(KeyCode.D))
             {
                 curMovement.x = walkSpeed;
                 myRB.velocity = curMovement;
             }
             else
             {
                 curMovement.x = 0f;
                 myRB.velocity = curMovement;
             }
 
             if (Input.GetKey(KeyCode.W))
             {
                 curMovement.y = walkSpeed;
                 myRB.velocity = curMovement;
             }
             else if (Input.GetKey(KeyCode.S))
             {
                 curMovement.y = -walkSpeed;
                 myRB.velocity = curMovement;
             }
             else
             {
                 curMovement.y = 0f;
                 myRB.velocity = curMovement;
             }
         }
         else
         {
             curMovement.x = 0f;
             curMovement.y = 0f;
             myRB.velocity = curMovement;
         }
     }
 
     void BorderChecker()
     {
         if (transform.position.x <= leftBorder)
         {
             transform.position = new Vector3(leftBorder, transform.position.y, 0);
         }
         if (transform.position.x >= rightBorder)
         {
             transform.position = new Vector3(rightBorder, transform.position.y, 0);
         }
         if (transform.position.y <= bottomBorder)
         {
             transform.position = new Vector3(transform.position.x, bottomBorder, 0);
         }
         if (transform.position.y >= topBorder)
         {
             transform.position = new Vector3(transform.position.x, topBorder, 0);
         }
     }
 
     void FireGun()
     {
         if (Input.GetKey(KeyCode.Space) && fireDelay == fireDelayReset)
         {
             hasFired = true;
             if (isFlipped == true)
             {
                 Instantiate(bulletPrefab, bulletSpawnLeft);
             } else if (isFlipped == false)
             {
                 Instantiate(bulletPrefab, bulletSpawnRight);
             }
         }
     }
 
     void PlayerFacing()
     {
         if (Input.GetKey(KeyCode.A))
         {
             mySR.flipX = true;
             isFlipped = true;
         }
         if (Input.GetKey(KeyCode.D))
         {
             mySR.flipX = false;
             isFlipped = false;
         }
     }
 
     void FireChecker()
     {
         if (hasFired == true)
         {
             fireDelay -= Time.deltaTime;
             if (fireDelay <= 0f)
             {
                 fireDelay = fireDelayReset;
                 hasFired = false;
             }
         }
     }
 
     void HitBoxChecker()
     {
         Debug.Log(Physics2D.OverlapArea(HitBoxTL.position, HitBoxBR.position, Enemy1, -300, 300));
         if (isFlipped == false)
         {
             isTouched = Physics2D.OverlapArea(HitBoxTL.position, HitBoxBR.position, Enemy1);
             //Enemy 1 Layer Check
             if (isTouched == true)
             {
                 Debug.Log("Enemy 1");
                 TakeDamage(En1Dmg);
             }
             //Enemy 2 Layer Check
             if (Physics2D.OverlapArea(HitBoxTL.position, HitBoxBR.position, Enemy2))
             {
                 TakeDamage(En2Dmg);
             }
             //Enemy 3 Layer Check
             if (Physics2D.OverlapArea(HitBoxTL.position, HitBoxBR.position, Enemy3))
             {
                 TakeDamage(En3Dmg);
             }
             //Boss 1 Layer Check
             if (Physics2D.OverlapArea(HitBoxTL.position, HitBoxBR.position, Boss1))
             {
                 TakeDamage(Boss1Dmg);
             }
             //Boss 2 Layer Check
             if (Physics2D.OverlapArea(HitBoxTL.position, HitBoxBR.position, Boss2))
             {
                 TakeDamage(Boss2Dmg);
             }
         } else
         {
             //Enemy 1 Layer Check
             if (isTouched == true)
             {
                 Debug.Log("Enemy 1");
                 TakeDamage(En1Dmg);
             }
             //Enemy 2 Layer Check
             if (Physics2D.OverlapArea(HitBoxTLFlipped.position, HitBoxBRFlipped.position, Enemy2))
             {
                 TakeDamage(En2Dmg);
             }
             //Enemy 3 Layer Check
             if (Physics2D.OverlapArea(HitBoxTLFlipped.position, HitBoxBRFlipped.position, Enemy3))
             {
                 TakeDamage(En3Dmg);
             }
             //Boss 1 Layer Check
             if (Physics2D.OverlapArea(HitBoxTLFlipped.position, HitBoxBRFlipped.position, Boss1))
             {
                 TakeDamage(Boss1Dmg);
             }
             //Boss 2 Layer Check
             if (Physics2D.OverlapArea(HitBoxTLFlipped.position, HitBoxBRFlipped.position, Boss2))
             {
                 TakeDamage(Boss2Dmg);
             }
         }
     }
 
     void TakeDamage(float dmg)
     {
         if (curArmor > 0)
         {
             curArmor -= dmg;
         } else
         {
             curHealth -= dmg;
         }
 
         if (curArmor < 0)
         {
             curHealth -= curArmor;
             curArmor = 0;
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

168 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D detecting if the player is someplace touching part of a particle system 0 Answers

Check not colliding when object has been destroyed,Check if object no longer colliding if it's been deleted 0 Answers

Trying to freeze multiple gameObjects at the same time 1 Answer

2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers

How to check Hypothetical Collisions in 2D 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges