Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shreevarshan822 · Mar 12, 2021 at 12:41 PM · errorraycastenemy1st personhealth

Raycast Shoot Error

My game is a simple FPS. What I'm trying to do is have it so that each time a raycast shot by the player hits an enemy, their health decreases by a defined amount. but when i shoot the enemy once, it dies i cant fix

This is the weapon script

using System.Collections; using System.Collections.Generic; using UnityEngine; using EZCameraShake; using TMPro;

public class sWeapon : MonoBehaviour { //Recoil [Header("Recoil")] public sRecoil recoil;

 //UI
 [Header("Text Mesh Pro UI Field")]
 public TextMeshProUGUI WeaponAmmoUI;

 //Camera Shake
 [Header("Camera Shake")]
 public CameraShaker CameraShake;

 //Audio
 [Header("Audio Settings")]
 public AudioSource ShootSound;
 public AudioSource ReloadSound;

 [Header("Particle System Settings")]
 //Particle System
 public ParticleSystem ShootMuzzleFlash;

 //Public Variables
 [Header("Weapon Configures")]
 public float damage = 10f;
 public float range = 200f;
 public float FireRate = 15f;

 //Ammo 
 [Header("Ammo Settings")]
 public int maxAmmo = 10;
 //Reload
 [Header("Reaload Settings")]
 public float reloadtime = 1.9f;
 private bool isReloading = false;
 private int CurrentAmmo;

 //Animator
 [Header("Animator")]

 public Animator animator;

 [Header("Camera")]
 public Camera fpsCamera;

 private float nextTimeTofire = 0f;


 void Start()
 {
     animator = gameObject.GetComponent<Animator>();
     CurrentAmmo = maxAmmo;
 }

 private void OnEnable()
 {
     isReloading = false;
     animator.SetBool("Reload", false);
 }

 void Update()
 {
     //UI TEXT
     WeaponAmmoUI.SetText(CurrentAmmo + " / " + maxAmmo);

     if (isReloading)
         return;

     if (CurrentAmmo <= 0)
     {
         StartCoroutine(Reload());
         return;
     }

     if (Input.GetButton("Fire1") && Time.time >= nextTimeTofire)
     {

         nextTimeTofire = Time.time + 1f / FireRate;
         Shoot();
     }
 }

 IEnumerator Reload()
 {
     isReloading = true;
     Debug.Log("Reloading..");

     ReloadSound.Play();
     animator.SetBool("Reload", true);

     yield return new WaitForSeconds(reloadtime);

     animator.SetBool("Reload", false);
     yield return new WaitForSeconds(0.5f);


     CurrentAmmo = maxAmmo;
     isReloading = false;
 }

 void Shoot()
 {
     if (isReloading)
         return;

     //Particles sound recoil// 
     ShootSound.Play();
     ShootMuzzleFlash.Play();
     recoil.Fire();

     //Camera Shake

     //Ammo Reducing
     CurrentAmmo--;

     RaycastHit hit;
     if (Physics.Raycast(fpsCamera.transform.position, fpsCamera.transform.forward, out hit, range))

     {
         Vector3 fpsCamera = transform.TransformDirection(Vector3.forward) * 10;
         Debug.DrawRay(transform.position, fpsCamera, Color.red);
         Debug.Log(hit.transform.name);

         sTarget target = hit.transform.GetComponent<sTarget>();
         if (target != null)
         {
             target.TakeDamage(damage);
         }
     }
 }

}

This is my enemy script

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class sTarget : MonoBehaviour { //Public Variables public float Health = 200f;

 public void TakeDamage (float amount)
 {
     Health =- amount;
     if (Health <= 0f)
     {
         Die();
     }

     void Die ()
     {
         Destroy(gameObject);
         Debug.Log("died");
     }

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AbandonedCrypt · Mar 12, 2021 at 01:03 PM

You are setting Health to -amount, which is automatically below zero for any positive damage value. what you are trying to do is health -= amount; not health =- amount;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shreevarshan822 · Mar 12, 2021 at 02:14 PM 0
Share

Thanks It took 24 hrs

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

198 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy Health With Raycast Not Working 1 Answer

enemy detect player then attack - c# 1 Answer

Raycasting help, enemy damage 0 Answers

How do I destroy a game object (The enemy Goblin Game Object) upon entering a collision box? (JavaScript) 2 Answers

Enemy Health Problems 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges