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 /
This question was closed Nov 04, 2016 at 11:10 PM by Zitoox for the following reason:

The question is answered, right answer was accepted

avatar image
-1
Question by Zitoox · Nov 04, 2016 at 09:54 PM · c#scripting problemtagdetection

Detect tag in C#

I was confortable in JavaScript making my Aim System, but unfortunately i discovered that i NEED to use C# on this, so i rewrited the script. The problem is that it is the biggest mass i've ever seen. I am a beginner at C# and i am learning about it, so this might be an easy thing but i don't know how to solve it =P

Have you ever seen a box inside a box inside another box? It's basically the same thing here. I want to make a script attached to an aim in which When the player click with the left mouse button, the script will detect if there is something colliding with the aim. If there IS something colliding with the aim, and that thing has the tag "Enemy", then a log will be displayed for testing purposes.

But i ended up making this mess:

 using UnityEngine;
 using System.Collections;
 
 public class AimSys : MonoBehaviour
 {
 
 
     bool Click; //This is a trigger.
 
     public float fireRate = 0.5F; //Fire rate.
     private float nextFire = 0.0F; //Fire rate.
 
     void Update()
     {
         if (Input.GetButton("Fire1") && Time.time > nextFire) //Shoots on fire rate. I've set it up to 2 seconds before shooting again.
         {
             Click = true; //If the player has shoot, THEN:
         }
     }
 
     void OnCollisionEnter(Aim collision) //This is where it gets a mess. This was supposed to detect if there is anything in the aim's collision area and THEN:
     {
         if (Click) // IF the player has shoot, THEN:
         {
             if (Aim.gameObject.tag == "Enemy") // Find if the object colliding with the aim is actually an enemy by finding it's tag.
             {
                 Debug.Log("TARGET HIT!"); //This is where i will implement the health reduction.
             }
         }
     }
 }

Can someone help me removing some {} and making it actually work? I would appreciate it. Principally by removing some keys or whatever is it called (Not my mother language sorry) This wouldn't happen if i was using Java =C

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

  • Sort: 
avatar image
0
Best Answer

Answer by jmgek · Nov 04, 2016 at 10:06 PM

  using UnityEngine;
  using System.Collections;
  
  public class AimSys : MonoBehaviour
  {
      bool Click = false; //This is a trigger.
      bool target = false;
      float timer;
  
      void Update()
      {
          if (target)
              if(Input.GetButton("Fire1")) 
              {
                  //logic goes here
              }
      }
  
      void OnCollisionStay(Collision collision) {
      {
         if(collision.gameobject.name == "Enemy")
              target = True;
      }

      void OnCollisionExit(Collision collision) {
         if(collision.gameobject.name == "Enemy")
             target = False;
     }
  }



Comment
Add comment · Show 4 · 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 Zitoox · Nov 04, 2016 at 10:48 PM -1
Share

alt text

errors.png (13.3 kB)
avatar image Zitoox · Nov 04, 2016 at 10:53 PM -1
Share

Ok, i've fixed it. There were some errors on your script and things that doesn't make sense at all...

 using UnityEngine;
 using System.Collections;
 
 public class AimSys : $$anonymous$$onoBehaviour
 {
     bool Click = false; //This is a trigger.
     bool target = false;
 
     public float fireRate = 0.5F;
     private float nextFire = 0.0F;
    
 
     void Update()
     {
         if (target)
             if (Input.GetButton("Fire1") && Time.time > nextFire)
             {
                 Debug.Log("Target Hit!");
             }
     }
 
     void OnCollisionEnter(Collision collision) 
     {
         {
             if (collision.gameObject.tag == "Enemy")
                 target = true;
         }
     }
 }
 

Unfortunatelly it ignores the fire rate.

avatar image jmgek Zitoox · Nov 04, 2016 at 11:04 PM 1
Share

Sorry wrote it on my Phone:

You might want to keep OnCollisionExit seeing how you're never setting target to false.

You're also going to want to put a timer somewhere in your Update, I am subtracting but you can do whatever you want.

              if(nextfire >= 0)
                      nextFire -= time.deltaTime; 
              if (Input.GetButton("Fire1") && nextFire <= 0)
              {
                  Debug.Log("Target Hit!");
                  nextFire = fireRate;
              }

It's not the cleanest but it should work, You can easily find fire mods online to do a clean job.

avatar image Zitoox jmgek · Nov 04, 2016 at 11:10 PM -1
Share

Ohh i forgot that part! It worked perfectly now, thanks! ^.^

Follow this Question

Answers Answers and Comments

241 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Aim ignoring script 1 Answer

Physics.OverlapSphere, if statement, gameObject.tag 0 Answers

How do keep the value of the Transform variable in script which is instantiated with a Game Object? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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