- Home /
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