I have a problem where I enable the collider when I do an attack and it doesn’t detect collisions. It detects when it starts enabled only. I know that I could store a boolean that says if you are attacking and that is checked when a collision occurs, but it is more optimized to only use the colliders sometimes and I don’t know why it doesn’t work.
Script that checks collisions:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dmg : MonoBehaviour
{
[SerializeField] float dmgv;
[SerializeField] GameObject MyPlayer;
void OnTriggerEnter2D(Collider2D col){
if(col.gameObject.GetComponent<Player>() != null && col.gameObject != MyPlayer){
col.gameObject.GetComponent<Player>().Knockback(dmgv, transform.localScale.x/Mathf.Abs(transform.localScale.x), MyPlayer.GetComponent<Player>().ducked);
Debug.Log("Hit");
}
}
}
Script that enables and disables it:
public void HitboxStart(){
hitboxes[attack].enabled = true;
}
public void HitboxEnd(){
hitboxes[attack].enabled = false;
}
My other problem is that I made a script to track the players and zoom in and out, but when the players get too close it suddenly zooms out.
Here is the script I put on the players:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Target : MonoBehaviour
{
TargetTracker TargetTracker;
// Start is called before the first frame update
void Start()
{
TargetTracker = FindObjectOfType<TargetTracker>();
}
// Update is called once per frame
void Update()
{
if(transform.position.x > TargetTracker.right.transform.position.x){
TargetTracker.right = gameObject;
}else if(transform.position.x < TargetTracker.left.transform.position.x){
TargetTracker.left = gameObject;
}else if(transform.position.y > TargetTracker.up.transform.position.y){
TargetTracker.up = gameObject;
}else if(transform.position.y < TargetTracker.down.transform.position.y){
TargetTracker.down = gameObject;
}
}
}
Here is the script I put on my camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetTracker : MonoBehaviour
{
public GameObject left;
public GameObject right;
public GameObject down;
public GameObject up;
public float boundleft;
public float boundright;
public float bounddown;
public float boundup;
[SerializeField] float padding;
Camera camera;
// Start is called before the first frame update
void Start()
{
camera = GetComponent<Camera>();
}
// Update is called once per frame
void Update()
{
float upf = up.transform.position.y;
float downf = down.transform.position.y;
float leftf = left.transform.position.x;
float rightf = right.transform.position.x;
if(upf>boundup){
upf=boundup;
}
if(downf<bounddown){
downf=bounddown;
}
if(leftf<boundleft){
leftf=boundleft;
}
if(rightf>boundright){
rightf=boundright;
}
if(rightf-leftf>upf-downf){
camera.orthographicSize = Screen.height/((Screen.width/(rightf-leftf))*2)+padding;
}else{
camera.orthographicSize = (upf-downf)/2+padding;
}
transform.position = new Vector3(leftf+((rightf-leftf)/2),downf+((upf-downf)/2),-10);
}
}