ApplyExplosionToRagdoll add Transform bullet and knockback the enemy

Hey I was thinking and wanted to ask how should I getComponent when it’s not on the prefab and doesn’t seem to be a good idea to attach gameobject to script in inspector.

Should I tryGetComponent or what is the best idea to find something like a bullet when matter of seconds and it’s not in the anymore.

I’m not sure I understand the question, GetComponent when it’s not on the prefab? If the prefab doesn’t have a component of that type it will return null

What do you mean by “finding something like a bullet”? What exactly are you attempting to do?

Sorry I explained it wrong let me try again.
Soo I want to get variables from the bulletProjectile (transform.poisition or some values).
And what is the best or the right way to get these things when it’s not in the same object.
And the bullet getting destroyed matter of seconds soo it’s not the proper way to search for the tag and get Components like that.

So if you have a reference to the GameObject that the component is on, then you can use that reference for the GetComponent. Things like the transform don’t require a GetComponent, for example, if you have a reference to the bulletProjectile, then you can get the position with

bulletProjectile.transform.position;

I know that but the point is that I want to get the Components of the bulletProjectile script. And what is the proper way to get that.
Should I use FindGameObjectsWithTag,trygetcomponent ?

Because this is a Turn based game, there should be just one instance of the bullet at any given time…
So you could tag the Projectile and use

GameObject.FindWithTag("Bullet").GetComponent<BulletProjectile>();

or

FindObjectOfType<BulletProjectile>();
1 Like

But from where are you trying to get that reference? What exactly are you trying to do?

Are you running code on the enemy and you need a reference to the bullet? Or are you running code on the player unit? Or running code on the bullet and need the reference of the hit object? Or or something else entirely?
The solution depends on exactly what you’re trying to do.

When the bullet is spawned you know where it’s going, you know the targetUnit. If you want the targetUnit to know what bullet is heading their way you could pass in the reference at that point.
Or if you want the bullet to know the targetUnit then you can pass that reference to the bullet at that point as well instead of just the targetPosition.

1 Like

I am looking at the title of the post and I’m not sure how to answer this. It seems you want to apply knockback to a Unit when a bullet hits them. Your question, though, is about getting hold of the bullet transform before it is destroyed.

I’m going to try and answer the post as a whole.

First, how do plan on doing the actual knockback? The ragdoll is not going to work because it’s just that: a ragdoll. You are going to have very little control over it unless you look into active ragdolls, and that’s a beast I shouldn’t even have mentioned. An animation could work, but will likely not need to know anything about the bullet unless you have some complex blending or procedural stuff happening.

Once you have figured that out, there are a few ways you could do this (with the code we have)

One way would be to have a ‘Knockback’ method on the unit that will do whatever you want to do. You would have to update the BulletProjectile a little so that it knows which unit it was targeting, but that’s not a big change. It will, however, restrict what you can shoot at (which isn’t so much a problem at the moment 'cos we can only shoot Units anyway, but the projectile isn’t applying that restriction, the ShootAction is).

You can pass the target unit to the Setup of the BulletProjectile

private Unit targetUnit;
public void Setup(Vector3 targetPosition, Unit targetUnit)
{
    // I am not getting targetPosition from the unit
    // transform because we are passing in a different
    // target position (so that we don't shoot the feet)
    this.targetPosition = targetPosition;
    this.targetUnit = targetUnit;
}

Now, when the bullet reaches the target and destroys itself, it can call the ‘Knockback’ method on Unit

if (distanceBeforeMoving < distanceAfterMoving)
{
    transform.position = targetPosition;

    trailRenderer.transform.parent = null;

    // I am passing moveDir to knockback so that the unit
    // can know the direction the bullet was travelling in. Could
    // be useful for applying directional force, for example
    targetUnit.Knockback(moveDir);

    Destroy(gameObject);

    Instantiate(bulletHitVfxPrefab, targetPosition, Quaternion.identity);

}

Example Knockback method

public void Knockback(Vector3 knockDirection)
{
    // UNTESTED
    // Try to move (teleport, really) the unit 1 tile away
    // in the direction the bullet is flying
    var targetPos = transform.position + (knockDirection * LevelGrid.Instance.GetCellSize());
    var targetGridPos = LevelGrid.Instance.GetGridPosition(targetPos);
    if (!LevelGrid.Instance.IsValidGridPosition(targetGridPos)) return;
    if (!Pathfinding.Instance.IsWalkableGridPosition(targetGridPos)) return;
    transform.position = LevelGrid.Instance.GetWorldPosition(targetGridPos);
}

If you definitely want the bullet transform, make the Knockback accept Transform instead of Vector3 and pass that instead of moveDir, but I’m not sure why you would need that. The Knockback function is called before the bullet gets destroyed, so you will have access to it.

1 Like

Privacy & Terms