- Home /
How to damage player when bullet collides with player.
I'm trying to make a super smash bros. like game and you shoot each other around the stage. I have a bullet spawn when either Fire1 or Fire2 is pressed and I need the bullet to damage the player when it collides with each other. Here's the scripts that may be associated with damaging the player:
This Rotates the gun: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GunRotate : MonoBehaviour {
public float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1"))
{
transform.Rotate(new Vector3(Time.deltaTime * -speed, 0, 0));
}
if (Input.GetButton("Fire2"))
{
transform.Rotate(new Vector3(Time.deltaTime * speed, 0, 0));
}
}
}
This makes the bullet get destroyed 5 seconds after colliding with the arena: using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Collections;
public class Bullet : MonoBehaviour { public GameObject bullet; public float amount; public float health; public Rigidbody rb;
// Start is called before the first frame update
void Start()
{
; }
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Box")
{
Debug.Log("Collision Detected");
Destroy (gameObject,5);
}
}
}
This spawns the bullet from the gun: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Collections;
public class GunShoot : MonoBehaviour {
public Transform firePoint;
public GameObject bullet;
public float firerate;
public GameObject muzzleFlash;
float muzzleCountdown;
float bulletCountdown;
private float nextTimeToFire;
public Rigidbody rb;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f/firerate;
Instantiate(bullet, firePoint.position, firePoint.rotation);
}
if (Input.GetButton("Fire2") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / firerate;
Instantiate(bullet, firePoint.position, firePoint.rotation);
}
}
}
This is the health( I didn't finish it because I'm trying to figure out this damage code):
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Health : MonoBehaviour {
public float health = 10000f;
public float amount = 10f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
} This is the arena( The gun is connected to the player):
Your answer
Follow this Question
Related Questions
Prefab shooting damage/health? 1 Answer
how to make enemy damage player? 1 Answer
Weapon Mesh Damage 1 Answer
How do I create projectile weapons that all have travel time? 0 Answers
Enemy Health, Player Damage 0 Answers