#how does code distinguish between
1 messages · Page 1 of 1 (latest)
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
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
Is the stopping distance lower than the damage distance?
yes but the collider that makes the player take dmg also stops the player from moving into the enemy
Is it a circle collider?
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?
yes these are the components
if i increase the radius which makes the player take dmg it also increases the hitbox of the enemy
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
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
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
Ok so you're saying the player gets hurt when he goes in the big one?
Should I set the middle collider to a trigger and then rename the oncollsionstay to ontriggerstay or does that not work?
This is what we're trying to fix
So basically I'm wondering what is causing that
well i didnt set the capsule collider to a trigger thats why i think?
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
I think i should make another collider which just acts as a hitbox and make that capsule collider a trigger
yeah
the hitbox should be on the enemy right?
Hitbox should be on the enemy root yes
but then you need two seperate Game Objects as a child of the enemy
also i tried to switch the capsule colldier to a circle one but it didnt work, does it mess with the other circle collider?
Didn't work in what way
No damage
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
can i remove this somehow
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
btw thank you so much for helping me i really appreciate it
Updated the script actually, you'll need a rigidbody on it
rigidbody on the vision gameobject?
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
My pleasure. God bless you. 🙂
Oops and make sure this is "2D" not just "Rigidbody"
[RequireComponent(typeof(Rigidbody2D))]
Sorry about that
should i add a gameobject to public EnemyPatrol patrol or should i make it private
in the inspector
Yeah you'll need to assign the EnemyPatrol reference to the patrol variable in the inspector
Do you mean i need to put the script EnemyPatrol in the variable because I can only put the enemy gameobject in it
Yeah that's fine it will automatically reference the script if you just drag the game object
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
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
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