3
\$\begingroup\$

I'm new to Entitas. I have a DamageSystem that should Physics.Raycast() toward the target and if tag is enemy it should reduce enemy health.

I have HealthComponent:

public class HealthComponent: IComponent{

public float value;

}

In DamageSystem I wanna do something like:

if(Physics.Raycast(target, direction, out hit)){

     tag.gameObject.entity.health.value = currentHealth - damage ;

}

But after Physics.Raycast(), I have only the gameObject and getEntityLink() method that return EntityLink without actual components... I guess that getEntityLink() returns only abstract components, and there is no way to get HealthComponent from EntityLink?

Does someone know how to get components from EntityLink, in a clean manner? Thanks!

\$\endgroup\$
0
\$\begingroup\$

Here is the answer

  if(Physics.Raycast(target, direction, out hit)){

        var target = hit.transform.gameObject.GetEntityLink().entity;

            HealthComponent component = ((HealthComponent)target.GetComponent(GameComponentsLookup.Health));
             //reduce health
            component.value -= weapon.weaponDamage;

            target.ReplaceComponent(GameComponentsLookup.Health, component);


    }

Thanks!

\$\endgroup\$
-1
\$\begingroup\$

First of all, You can read about Physics.Raycast Here. Second of all, You need to store the data that the Physics.Raycast outputs.

I will keep it really simple but you should use layermasks with Raycast to avoid hitting colliders and triggers that can stop the raycast from hitting the target.

How layers work can be found Here. More about Layermasks you can read Here.

So the code should look something like this:

void Shoot(int damage) //Function that shoots raycast
{
  RaycastHit hit; //variable to store raycast data
  Enemy enemy; //Enemy component that will be stored and used

  if(Physics.Raycast(transform.position, transform.forward, out hit)) //We shoot the ray
  {
    if(hit.collider.tag == "Enemy") //We check if what we hit has an Enemy Tag
       enemy = hit.collider.gameObject.GetComponent<Enemy>(); //We store that component to use later.

    if(enemy != null) //We check if the component is existing and not empty
       enemy.TakeDamage(damage); //We apply damage based on value the function input damage
  }
}

As for the health system there should be a function that takes the damage.

private int _health = 100; //Private variable so it won't be changed from outside the script


public void TakeDamage(int damage) //A simple function to apply damage, its public so other scripts can access it and use it to damage the health system
{
  _health -= damage; //We apply the damage that was given to the function.
}
\$\endgroup\$
1
  • \$\begingroup\$ I wanna get EntityLink() , not c#-script \$\endgroup\$ – lazarevsky Apr 2 '18 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.