The problem is this: I want to change the direction of the fall of an object, to fit where the raycast (bullet in this case) is coming from. My code so far is this:
public class Target: MonoBehaviour
{
public float health = 20f;
public Rigidbody rb;
bool dead = false;
public void TakeDamage(float damage)
{
health -= damage;
if (health <= 0f)
{
Die();
}
}
void Die()
{
if (dead == false)
{
dead = true;
rb.AddForce(20, 0, 0);
}
}
}
The rb.AddForce needs to be rotated, to fit where the raycast is coming from.
UPDATE
if (dead == false)
{
dead = true;
rb.AddForce(rb.position - shooter.position);
}
this fixes the things, and also i added the shooter transform
Assuming I understood your question (SO's rules don't let me ask for clarification yet, I can only post hard answers), you could just modify your TakeDamage() function so that it took in a Ray. Then, from wherever you're actually firing the Ray (your gun or player script, I'm guessing), just pass it into TakeDamage().
To be clear though, this will add force in the direction of the Ray, so your Target will be pushed in the direction the bullet was going. You may be looking for more complex behavior. If you want the target to react realistically based on where the bullet hits, look at AddForceAtPostion, which will allow you to both push the Target, as well as spin it realistically.
public void TakeDamage(float damage, Ray rc)
{
health -= damage;
if (health <= 0f)
{
Die(rc);
}
}
void Die(Ray rc)
{
if (dead == false)
{
dead = true;
rb.AddForce(rc.direction);
}
}