Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Joseph8706 · Jan 25, 2021 at 05:35 PM · collisionraycastclone

Any collision with my cloned objects only affects the last clone created

I have an enemy object with scripts and components and a figure. I clone this object when the game starts. I have an empty spawner game object that creates these clones and a raycast from my gun object that decreases their health. For some reason, no matter which clone I hit, it only decreases the health of the last clone created. Also, even when the last clones health drops below zero, it doesn't destroy itself as my code says it should.

The code for the spawner:

 public class Spawner : MonoBehaviour
 {
     public GameObject prefab;
     public GameObject clone;
 
     public int maxEnemies = 10;
     public int enemies = 0;
 
     void Update()
     {
         if(enemies < maxEnemies){
             clone = (GameObject)Instantiate(prefab, transform.position, Quaternion.identity);
             enemies += 1;
         }
     }
 }

The code for the enemy:

 public class Mutant : MonoBehaviour
 {
     public Character player;
     public Transform enemyTransfrom;
     public Spawner spawner;
 
     public float speed = 10f;
     public float maxDis = 0.5f;
     public float minDis = 1.5f;
     public float health = 10;
     public bool dying = false;
     
     void Update()
     {
         if(health > 0){
             transform.LookAt(player.transform);
             
             if(Vector3.Distance(enemyTransfrom.position, player.transform.position) > minDis){
                 enemyTransfrom.position += enemyTransfrom.forward * speed * Time.deltaTime;
                 speed = 30f;
 
                 if(Vector3.Distance(enemyTransfrom.position, player.transform.position) <= maxDis){
                     speed = 10f;
                     fighting = true;
                 }else{
                     fighting = false;
                 }
             }
         }else{
             dying = true;
             speed = 0f;
             fighting = false;
         }
     }

 public void Die(){
     Object.Destroy(spawner.clone);
     spawner.enemies -= 1;
 }

When dying is true, it starts an animation that runs the function die() at the end of it.

Finally, the code for the raycast:

     private Vector3 direction; 
 
     public Transform shootPoint;
     public Transform blaster;
     public Transform shotTarget;
 
     void Update()
     {
         direction = shotTarget.position-transform.position;
         Debug.DrawRay(shootPoint.position, direction, Color.red);
         RaycastHit hit;
         Ray beam = new Ray(blaster.position, direction);
         if(Physics.Raycast(beam, out hit, 1000)){
             if(hit.collider.tag == "Enemy"){
                 enemy.health -= 10;
             }
         }

I've been struggling with this for a while and any help would be greatly appreciated. Thanks!

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

Answer by GDGames0302 · Jan 26, 2021 at 07:42 AM

In the Die fuction you should do Destroy(this.gameobject).

public void Die() { spawner.enemies -= 1; Destroy(this.gameobject); } In this case you destroy this gameobject when its health is less than 0.

Also, in the raycast script, you can try something like this:

if(hit.collider.tag == "Enemy") { hit.collider.tag.gameobject.GetComponent Mutant().health -= 10; } For some reason, you can't type "greater" or "less" signs over here, but you should do: if(hit.collider.tag == "Enemy") { hit.collider.tag.gameobject.GetComponent "less sign"Mutant"greater sign"().health -= 10; }

In this case you deal damage only to the enemy that you hit. This only works if all gameobjects with Enemy tag have Mutant script attached. If not all gameobjects with Enemy tag have Mutant script attached, you can either check if the hit gameobject has Mutant script attached, or you can put another tag on enemies that don't have Mutant script attached.

Comment
Add comment · 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

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

213 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 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

Jumping with raycasts causing issues? 2 Answers

Raycast bullet impact and instantiating particles 3 Answers

Can someone modify this for me? 0 Answers

Scripted Door w/ Line of Sight 1 Answer

How to check if a raycasthit detects an angle less than 45 degrees and touches the ground 2 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