#how does code distinguish between

1 messages · Page 1 of 1 (latest)

dusk oyster
#

Basically if the collider attached to this game object (with the script on it) impacts another collider, it reports the collision info for the collider that this object hit.

#

What if i have multiple colliders on my gameobject?
Same thing

#

If any of those child colliders hits another collider, it will report on whichever script is attached to that collider (the source of the hit)

#

Unless you have a rigidbody

wanton cosmos
#

Ok im kinda stuck on making the stoppingdistance and the radius in which the player takes dmg and im pretty sure it should be simple

dusk oyster
#

Is the stopping distance lower than the damage distance?

wanton cosmos
#

yes but the collider that makes the player take dmg also stops the player from moving into the enemy

dusk oyster
#

Is it a circle collider?

wanton cosmos
#

1 circle collider and 1 capsule

#

idk why it shows 3 tho

dusk oyster
#

Make sure to reduce the radius then or make it so your radius is larger than the player's collision radius

#

Hmmm you said capsule right?

wanton cosmos
#

yes these are the components

#

if i increase the radius which makes the player take dmg it also increases the hitbox of the enemy

dusk oyster
#

Ahhh I see what's wrong

#

Are you using OnTriggerEnter for damage?

wanton cosmos
#
using System.Collections.Generic;
using UnityEngine;

public class EnemyPatrol : MonoBehaviour
{

    public int speed = 3;
    private Transform target;
    [SerializeField] private int attackDamage = 10;
    [SerializeField] private int attackSpeed = 1;
    private float canAttack;
    public GameObject Player;
    [SerializeField] private float stoppingDistance;


    private void Update ()
    {
        if (target !=null)
        {
            float step = speed * Time.deltaTime;

            if (Vector2.Distance(transform.position, target.position) > stoppingDistance)
            {
                transform.position = Vector2.MoveTowards(transform.position, target.position, step);
            }
            
        }

    }
    private void OnCollisionStay2D(Collision2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            if (attackSpeed <= canAttack )
            {
                other.gameObject.GetComponent<PlayerHealth>().UpdateHealth(-attackDamage);
                canAttack = 0f;
            }
            else
            {
                canAttack += Time.deltaTime;
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            target = other.transform;
            
        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            target = null;
        }
    }
  


}

Im using oncolllisionstay

dusk oyster
#

Ok interesting. I think you should move the damage collider to an empty game object and make it a child of the zombie

#

the trigger one

wanton cosmos
#

the big one is the trigger which makes the enemy chase and the middle one is the hitbox/take dmg idk why there is a 3rd one tho

dusk oyster
#

Ok so you're saying the player gets hurt when he goes in the big one?

wanton cosmos
#

no the middle one

#

the big collider is where the enemy starts chasing

wanton cosmos
dusk oyster
#

So basically I'm wondering what is causing that

wanton cosmos
#

well i didnt set the capsule collider to a trigger thats why i think?

dusk oyster
#

Ohhhhh I see

#

Is that the desired behavior?

#

Ohhhh

#

wait I'm getting it

#

the cogs in my brain are turning now

#

Hmmm

#

Lemme think about that real quick

wanton cosmos
#

I think i should make another collider which just acts as a hitbox and make that capsule collider a trigger

dusk oyster
#

Yea...

#

Yea basically you need a damage trigger, a vision trigger, and a hitbox

wanton cosmos
#

yeah

dusk oyster
#

But each of these should be different game objects

#

So you can differentiate them

wanton cosmos
#

the hitbox should be on the enemy right?

dusk oyster
#

Hitbox should be on the enemy root yes

#

but then you need two seperate Game Objects as a child of the enemy

wanton cosmos
#

also i tried to switch the capsule colldier to a circle one but it didnt work, does it mess with the other circle collider?

dusk oyster
#

Didn't work in what way

wanton cosmos
#

No damage

dusk oyster
#

Hmm no it shouldn't change anything

#

Basically make the zombie have a circle collider (non trigger) on the root game object

#

Create 2 Empty Game Objects with Circle colliders that are set to isTrigger = true

#

And you're gonna need two scripts, one that handles vision, and another that handles damage

wanton cosmos
#

can i remove this somehow

dusk oyster
#

Yes, just right click it and apply to prefab

#

it's trying to tell you it exists on the prefab

#
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class EnemyVision : MonoBehaviour
{
  public EnemeyPatrol patrol;

  void Awake()
  {
    GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
  }

  void OnTriggerEnter2D(Collider2D other)
  {
    if (other.tag == "Player")
    {
      patrol.target = other.transform;
    }
  }

  void OnTriggerExit2D(Collider2D other)
  {
    if (other.transform == patrol.target)
    {
      patrol.target = null;
    }
  }
}
#

Put this on the vision game object

wanton cosmos
#

btw thank you so much for helping me i really appreciate it

dusk oyster
#

Updated the script actually, you'll need a rigidbody on it

wanton cosmos
#

rigidbody on the vision gameobject?

dusk oyster
#

Yeah and make it kinematic

#

Because if you have rigidbody on the root game object it will make the OnCollision/OnTrigger events get called on the object with the nearest rigidbody in the hierarchy.

#

So we're trying to prevent the OnTrigger/OnCollision events from getting called on the root of the zombie object

dusk oyster
#

Oops and make sure this is "2D" not just "Rigidbody"

#

[RequireComponent(typeof(Rigidbody2D))]

#

Sorry about that

wanton cosmos
#

should i add a gameobject to public EnemyPatrol patrol or should i make it private

#

in the inspector

dusk oyster
#

Yeah you'll need to assign the EnemyPatrol reference to the patrol variable in the inspector

wanton cosmos
#

Do you mean i need to put the script EnemyPatrol in the variable because I can only put the enemy gameobject in it

dusk oyster
#

Yeah that's fine it will automatically reference the script if you just drag the game object

wanton cosmos
#

yeah i did that and it didnt work but the circle collider radius was 0 im so dumbbbb

#

Should i just rewrite the enemypatrol script to only have the dmg code or make a new script with dmg

#

oh nvm i need to make a new one and delete the dmg in enemypatrol

dusk oyster
#

Yepp

#

You got it

#

I have to run for a bit but I'll be back to check on this thread

wanton cosmos
#

Ok good news everything is working but the the player only takes dmg if i move in the radius and if i just stand still i dont take dmg

#

I should probably make it a ontriggerenter instead of stay

#

ok nvm it should be ontriggerstay but its not working

#

I have to completely leave every radius of the enemy and then reenter it to take dmg idk whats wrong

wanton cosmos
#

Idk if you see this but i fixed everything except the first problem you helped me with with stoppingDistance, it only works horizontally and not vertically so, if im left or rightt to the enemy it works but if im above the enemy or under he still pushes me, its midnight for me now and i need to study for something else so i will continue tmrw if you still want to help me. Thanks for all the help i appreciate it a lot

dusk oyster
#

I sent you a FR, and good night friend

#

You are probably Asia time I'm guessing or UK? I'm eastern standard time. So tomorrow for you it might be around afternoon or evening but I'll be online!

#

And no problem, this was the kind of stuff I wish I had someone to help me with too