Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Tatsumi-Kun · Jan 03, 2016 at 09:12 AM · collisionscript.raycasthitenemy aienemy health

How to have BulletPrefab do the damage in instead of RayCastHit

I want the bullet prefab do the damage like have the bullet enter the collidision to decrease the enemy Hp and when the OnTriggerEnter use for PlayerInRange but when i shoot the bullet it collide with the enemy and it move, the enemy have two collider box and capsule collider which is for the PlayerInRange and i want the box for the dmage. when i a enter the enemy capsule collider i notice my health decrease instead of the bullet that the enemy instantiate, i use lookAt but when my enemy want to shoot it seem to go in the wrong direction i mean the ShotSpawnPoint, my character not getting to decrease the enemy health because i using a bullet prefab which i don't know how to tag to do the damage cause i using raycast, the bullet shoot before the raycasthit but i don't want the raycast do the damage but the bullet so i don't know what to do here my scripts.

using UnityEngine; using System.Collections;

public class PlayerShooting : MonoBehaviour {

 public int damagePerShot = 20;
 public float timeBetweenBullets = 0.15f;
 public GameObject shotPrefab;
 public float range = 100f;
 public Transform shotSpawn;
 private AudioSource playershot;
 
 
 
 float timer;
 //Ray shootRay;
 //RaycastHit shootHit;
 int ShootableMask;
 void Start ()
 {
     ShootableMask = LayerMask.GetMask ("Shootable");
     playershot = GetComponent < AudioSource> ();
 }
   
 
 void Update ()
 {
     timer += Time.deltaTime;
     if (Input.GetButton ("Fire1") && timer >= timeBetweenBullets)
     {
         Shoot ();
     }
 }
 
 void Shoot ()
 {
     timer = 0f;
     RaycastHit hit;
     Bullet ();
     Ray ray = new Ray (shotSpawn.position, transform.forward);
     if (Physics.Raycast (ray, out hit, range))
     {
         if (hit.collider.tag == "Enemy")
         //EnemyHealth enemyHealth = hit.collider.GetComponent<EnemyHealth> ();
 
         //if (enemyHealth != null)
         //{
             //enemyHealth.TakeDamge (damagePerShot);
 
         //}
         Debug.DrawRay (shotSpawn.position, transform.forward * range, Color.green);
     }
 }
 void  Bullet()
 {
     GameObject shot = Instantiate (shotPrefab, shotSpawn.position, shotSpawn.rotation) as GameObject;;
 
     playershot.Play ();
 }

}

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class PlayerHealth : MonoBehaviour {

 public int startingHealth = 100;
 public int currentHealth;
 public Slider healthSlider;
 bool isDead;
 bool damaged;
 PlayerMovement playerMovement;
 PlayerShooting playershooting;
 public GameObject PlayerExplosion;
 private AudioSource playerExplosion;
 
 
 
 void Start ()
 {
     playershooting = GetComponent<PlayerShooting> ();
     playerMovement = GetComponent<PlayerMovement> ();
     currentHealth = startingHealth;
     playerExplosion = GetComponent<AudioSource> ();
 }
   
 public void TakeDamage(int amount)
 {
     currentHealth -= amount;
     healthSlider.value = currentHealth;
     if (currentHealth < 0 && !isDead) {
         Death ();
     }
    
 }
 
 public void Death () {
     isDead = true;
     Instantiate (PlayerExplosion, transform.position, transform.rotation);
     playerExplosion.Play ();
     playerMovement.enabled = false;
     playershooting.enabled = false;
 
 }
 // Update is called once per frame
 void Update () {
   
 }

}

using UnityEngine; using System.Collections;

public class EnemyAttack : MonoBehaviour {

 public float timeBetweenAttacks = 0.5f;
 public int attackDamage = 10;
 bool playerInRange;
 float timer;
 GameObject player;
 PlayerHealth playerHealth;
 EnemyHealth enemyHealth;
 public Transform shotSpawn;
 public GameObject enemyShot;
 
 void Start ()
 {
     player = GameObject.FindGameObjectWithTag("Player");
     playerHealth = player.GetComponent<PlayerHealth> ();
     enemyHealth = GetComponent<EnemyHealth> ();
 }
 
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject == player)
     {
         playerInRange = true;
     }
 }
 
 void OnTriggerExit (Collider other)
 {
   
     if (other.gameObject == player) {
         playerInRange = false;
     }
 }
 
 
 void Update ()
 {
     timer += Time.deltaTime;
     if (timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
     {
         Attack ();
     }
 }
 
 void Attack ()
 {
     timer = 0f;
     Instantiate (enemyShot, shotSpawn.position, shotSpawn.rotation);
     if (playerHealth.currentHealth > 0) {
         playerHealth.TakeDamage (attackDamage);
     }
 
 }

}

using UnityEngine; using System.Collections;

public class EnemyHealth : MonoBehaviour {

 public int startingHealth = 100;
 public int currentHealth;
 public GameObject enemyExplosion;
 
 CapsuleCollider capsuleCollider;
 bool isDead;
 void Start ()
 {
     capsuleCollider = GetComponent<CapsuleCollider> ();
     currentHealth = startingHealth;
 }
   
 // Update is called once per frame
 void Update () {
   
 }
 public void TakeDamge(int amount)
 {
   
     if (isDead)
         return;
     currentHealth -= amount;
 
     if (currentHealth < 0) {
         Death ();
     }
 }
 void Death ()
 {
     isDead = true;
     capsuleCollider.isTrigger = true;
     Instantiate (enemyExplosion, transform.position, transform.rotation);
    
 }

}

using UnityEngine; using System.Collections;

public class EnemyMovement : MonoBehaviour {

 Transform player;
 PlayerHealth playerHealth;
 EnemyHealth enemyHealth;
 public float damping;
 public float Speed = 10;
 void Start () {
     player = GameObject.FindGameObjectWithTag ("Player").transform;
     playerHealth = player.GetComponent<PlayerHealth> ();
     enemyHealth = GetComponent<EnemyHealth> ();
 }
   
 // Update is called once per frame
 void Update ()
 {
     if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
     {
         lookAtPlayer ();
         Movement ();
     }
 }
 void lookAtPlayer ()
 {
     Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
     transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
 }
 
 void Movement ()
 {
     transform.position = Vector3.MoveTowards (transform.position, player.position, Speed * Time.deltaTime);
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

I am making a FPS and I was wondering how i could make it so when the enemy gets close enough he starts to deal damage. 0 Answers

Making collision boxes objects can go through 1 Answer

My enemies stack and do not use gravity 2 Answers

Makign an object collide continously 1 Answer

Fall collision force with help of raycast 0 Answers


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