- Home /
Help getting Raycast last hit data
I'm making an FPS game, but I'm using Raycast to shoot. It takes about 5-8 shots to kill, how do i make the game say "this player's Raycast destroyed the object, so the get the kill (credit for object destroy)"
You need to look at Unity Event. You can call an event that have attacker and hit object. If hit object dead tell game that the last attacker kill me
how would i do this in c#, i get the schematic but idk how to write it out
You can take a look at this video here: https://unity3d.com/learn/tutorials/topics/scripting/events
Events are real simple and fun!!
Answer by b1gry4n · Jun 08, 2018 at 08:26 PM
when you damage the object, assuming you send some sort of damage message to the object, you can pass the players info along with the damage amount. if youre using send message for example...
public class DamageInfo
{
public float damageAmount;
public PlayerScript playerScript;
public DamageInfo(float d, PlayerScript p)
{
playerScript = p;
damageAmount = d;
}
}
void CastRay()
{
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(your ray setup))
{
hitInfo.transform.SendMessage("Damage", new DamageInfo(25, playerScript));
}
}
and then on the object youre hitting...
public void Damage(DamageInfo info)
{
float damage = info.damageAmount;
PlayerScript fromPlayer = info.playerScript;
currentHp -= damage;
if(currentHp <= 0)
{
Debug.Log(fromPlayer.playersName + " killed me");
}
}
Answer by CadenGS_ · Jun 09, 2018 at 08:51 PM
I believe i have something like this but I'm not sure, here are my scripts:
Shoot (Doing Damage):
void shoot()
{
currentAmmo--;
RaycastHit hit;
if (Physics.Raycast(player.transform.position, player.transform.forward, out hit, range))
{
Health health = hit.transform.GetComponent<Health>();
if (health != null)
{
health.TakeDamage(damage);
}
GameObject impactGo = Instantiate(impact, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGo, 1f);
}
and Health (Taking Damage):
public void TakeDamage(float amount)
{
health -= amount;
if(health <= 0f)
{
die();
}
}
I will link my FULL script below
P.S. probably the best and most detailed response I've ever given
Your answer
Follow this Question
Related Questions
Raycast Gun Fire Rate 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to store player model and then swap the model to an object I Raycast hit? 0 Answers
Problem with raycast 2 Answers