Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Need help im new to coding

Discussion in 'Scripting' started by rtalampas24, Jul 19, 2020.

  1. rtalampas24

    rtalampas24

    Joined:
    Jul 9, 2020
    Posts:
    3
    Hi there
    Basically I want it to hurt my player then kill the player. Also I want my players bullet to damage the enemy overtime then die.

    any help would be greatly appreciated many thanks
    peace, love, positivity
    Player Shoot Script

    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Diagnostics;
    using System.Runtime;
    using UnityEngine;
    public class PlayerShooting : MonoBehaviour
    {
    public GameObject BulletPrefab;
    public Transform BulletSpawn;
    public float damage = 5f;
    public float TimeBetweenShots = 0.3333f;
    public float m_timeStamp = 0f;
    // Ammo
    public int maxAmmo = 10;
    private int currentAmmo;
    public float reloadTime = 1f;
    private bool isReloading = false;
    //Gun Damage

    //Crosshair


    void Start ()
    {
    // Ammo
    currentAmmo = maxAmmo;
    //Crosshiar

    }


    void FixedUpdate ()
    {
    // Ammo
    if (isReloading)
    return;
    if (currentAmmo <= 0)
    {
    StartCoroutine(Reload());
    return;
    }

    if ((Time.time >= m_timeStamp) && (Input.GetKey(KeyCode.Mouse0)))
    {
    Fire();
    m_timeStamp = Time.time + TimeBetweenShots;
    }
    //Crosshair

    }
    // Ammo
    IEnumerator Reload()
    {

    isReloading = true;
    yield return new WaitForSeconds(reloadTime);
    currentAmmo = maxAmmo;
    isReloading = false;
    }
    void Fire ()
    {
    // Ammo
    currentAmmo--;

    var bullet = (GameObject)Instantiate(BulletPrefab, BulletSpawn.position, BulletSpawn.rotation);
    // Add velocity to the bullet
    bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 50 ;
    // Destroy thebullet after 2 seconds
    Destroy(bullet, 1.0f);
    //Enemy Target Instead of "hit" I had to put "bullet"

    }
    }


    Enemy Attack Script
    using System.Collections;
    using System.Collections.Generic;
    using System.Security.Cryptography;
    using UnityEngine;
    public class EnemyAttack : MonoBehaviour {
    //public float damage = 5f;
    [SerializeField] private Transform player;
    [SerializeField] private Transform Spawn;
    private void OnTriggerEnter(Collider other)
    {

    if (other.tag == "Player")
    {
    Destroy(other.gameObject);
    }
    else if(other.tag == "Bullet")
    {
    //Score Count what if i can add the same line for the player???
    Destroy(gameObject);
    ScoreCount.scoreValue += 10;
    }
    }
    }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Hey, first of all please use code tags.
    As i can see you dont store health anywhere. So start with that. You could make it its own component. When you collide with something and checked if its an enemy or the player, get this health component and subtract the damage from the health value. Damage over time would be handled slightly different, but that's a good place to get started. You should be able to find plenty of tutorials on these topics tho, so check some of them on youtube of other sites.
     
    rtalampas24 likes this.
  3. rtalampas24

    rtalampas24

    Joined:
    Jul 9, 2020
    Posts:
    3

    Ohh okay

    many thanks