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 /
  • Help Room /
avatar image
0
Question by The-IT-Nation · Jul 23, 2019 at 03:26 PM · scripting problemscript.programming

1 script for multiple enemy

i am referencing enemy script from player script and then when enemy is in range i can attack him , but when i drag multiple enemies i can attack only the referenced script but not others

Comment
Add comment · Show 4
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 aditya23dedhia · Jul 23, 2019 at 10:57 PM 0
Share

Are you using raycast system?

avatar image The-IT-Nation aditya23dedhia · Jul 24, 2019 at 03:46 PM 0
Share

No , I Am Not Using raycasts

avatar image Cornelis-de-Jager · Jul 24, 2019 at 02:32 AM 0
Share

Please add your code

avatar image Owen-Reynolds · Jul 24, 2019 at 05:35 PM 0
Share

Each enemy has a personal copy of that script. If you have 10 enemies, there are 10 copies. The player points to one particular copy, on one particular enemy.

For a hint how to find different enemies to point at, maybe look up "find nearest enemy".

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by The-IT-Nation · Jul 24, 2019 at 03:52 PM

@Cornelis-de-Jager @aditya23dedhia Enemy Code

 public class Enemy : MonoBehaviour
 {
     public float health = 100f;
     public float walkSpeed = 1.0f;      
     public float wallLeft = 0.0f; 
     public float wallRight = 5.0f;
     public float walkingDirection = 1.0f;
     Vector2 walkAmount;
     float originalX;
     public GameObject deathParticles;
     public Player Player;
     CircleCollider2D collid;
     public float waitTime = .5f;
     public bool playerInRange;
     void Start()
     {
         playerInRange = false;
         collid = GetComponent<CircleCollider2D>();
         this.originalX = this.transform.position.x;
     }
 
     void Awake() {
         
     }
 
     
     void Update() {
 
         if (walkingDirection > 0.0f && transform.position.x >= originalX + wallRight) {
 
             walkingDirection = -1.0f;
             Vector2 temp = transform.localScale;
             temp.x *= walkingDirection;
             transform.localScale = temp;
 
         } else if (walkingDirection < 0.0f && transform.position.x <= originalX - wallLeft) {
 
             walkingDirection = 1.0f;
             Vector2 temp = transform.localScale;
             temp.x *= -walkingDirection;
             transform.localScale = temp;
 
         }
 
         walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
 
         transform.Translate(walkAmount);
 
         if (health <= 0)
         {
             Death();
         }
 
     }
     void Death() {
         Instantiate (deathParticles, transform.position, transform.rotation);
         Destroy(gameObject);
     }
 
 
     void OnCollisionEnter2D(Collision2D other) {
         if (other.gameObject.tag == "Player") {
             Player.Death();
         }    
     }
     
 }

Player Code

 public class Player : MonoBehaviour
 {
     public CharacterController2D controller;
     public Enemy enemy;
     public float runSpeed;
     public float crouchSpeed;
     public float whileAttackSpeed;
     public float damage;
     public GameObject deathParticles;
     //public ParticleSystem walkingParticles;
     [System.NonSerialized]public bool dead;
     bool jump;
     bool run;
     bool crouch;
     bool attack;
     bool runB;
     bool enemyIsInRange;
     bool grounded;
     float runSpeedBak;
     float hMove;
     int indt;
     Animator anim; 
     Rigidbody2D rb;
     float waitBeforeAttack = 1.5f;
     void Start()
     {
         enemyIsInRange = false;
 
         rb = GetComponent<Rigidbody2D>();
 
         anim = GetComponent<Animator>();
 
         run = false;
 
         dead = false;
 
         jump = false;
 
         crouch = false;
         grounded = true;
 
         runSpeedBak = runSpeed;
     }
 
     
     void Update()
     {
         
 
         if (Input.GetKeyDown(KeyCode.Space)) {
 
             jump = true;
 
             anim.SetTrigger("Jump");
 
             grounded = false;
 
         }
 
         if (Input.GetKeyDown(KeyCode.Z)) {
 
             anim.SetTrigger("Attack");
 
             attack = true;
 
             rb.constraints = RigidbodyConstraints2D.FreezeAll;
 
             if (enemyIsInRange == true) {
                 
                 enemy.health -= damage;
                 Debug.Log(enemy.health);
 
             }
         }
 
         
         if (Input.GetKey(KeyCode.C)) {
 
             crouch = true;
 
             runSpeed = crouchSpeed;
 
             anim.SetBool("Crouch", true);
 
         } else {
 
             if (Input.GetKeyUp(KeyCode.C)) {
 
                 crouch = false;
 
                 runSpeed = runSpeedBak;
 
                 anim.SetBool("Crouch", false);
 
             }
             
         }
 
         hMove = Input.GetAxisRaw("Horizontal");
         
 
         if (hMove != 0) {
 
             run = true;
 
             anim.SetBool("IsRunning", true);
 
         } else {
 
             run = false;
 
             anim.SetBool("IsRunning", false);
 
         }
 
         
     }
 
     void FixedUpdate() {
         
         controller.Move(hMove * runSpeed * Time.deltaTime, crouch, jump);
 
         jump = false;
 
     }
 
     public void Death() {
 
         Instantiate (deathParticles, transform.position, transform.rotation);
         
         Destroy (gameObject);
 
         dead = true;
         
     }
 
     public void OnLandEvent() {
         grounded = true;
 
        //anim.Play("Idle");
        //anim.SetBool("JumpB", false);
 
     }
 
     void OnTriggerEnter2D(Collider2D col) {
 
         GameObject objectCollided = col.gameObject;
         if (objectCollided.CompareTag("Enemy")) {
             
             enemyIsInRange = true; 
         }
     }
     void OnTriggerExit2D(Collider2D col) {
 
         if (col.tag == "Enemy") {
 
             enemyIsInRange = false;
 
         }
     }
 
     public void attackOver() {
 
         rb.constraints = RigidbodyConstraints2D.FreezeRotation;
 
         attack = false;
 
     }
 
 }
 
 



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

206 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

Related Questions

Script to random set active a child object? 0 Answers

How to reference script in unity (C#) ? 0 Answers

UnityCar - Controlling Speed with a duration? 2 Answers

Replace dead npc with ragdoll ? 0 Answers

How can I have the ability to die as well as win the game at the same time? 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