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
-1
Question by Zitoox · Nov 06, 2016 at 08:39 PM · c#scripting problemtagaimingignorecollision

Aim ignoring script

As i said before, i am making an Aim System and i am getting humiliated here because my knowledge about C# is poor and i can't do this in Javascript apparently.

So here is my Script:

 using UnityEngine;
 using System.Collections;
 
 public class AimSys : MonoBehaviour
 {
     bool Click = false; //This is a trigger.
     bool target = false;
 
     public float fireRate = 0.5F;
     private float nextFire = 0.0F;
     public GameObject Flash;
 
 
     void Update()
     {
         if (target = true)
 
             nextFire -= Time.deltaTime;
         if (Input.GetButton("Fire1") && nextFire <= 0)
         {
             Flash.SetActive(true);
             Debug.Log("Target Hit!");
             nextFire = fireRate;
         }
 
     }
 
     void OnCollisionEnter(Collision collision)
     {
         {
             if (collision.gameObject.tag == "Enemy")
                 target = true;
         }
     }
 
     void OnCollisionExit(Collision collision)
     {
         {
             if (collision.gameObject.tag == "Enemy")
                 target = false;
             Flash.SetActive(false);
         }
     }
 }
 

This was supposed to do the following: There is a cillinder in front of my player's weapon that is an Aim. In every frame the Aim will try to detect if there is something colliding with it. If there is something colliding with the Aim, it will search the object for the tag "Enemy". If it has the tag "Enemy", then for now a simple Log will show. Later i plan to replace the log by a small life reduce in the Enemy life bar.

The problem is that my Aim is detecting EVERYTHING as an Enemy somehow. It doesn't ignore my fire rate, so it properly shoots every two seconds if i am pressing the mouse button (Auto-Fire). I think there is something wrong with the Update Function. I can shoot everywhere, no matter if i am hitting a collider or not, and the log will be shown. It seems that the collision is being ignored but still the log is being displaced. Also the flash that i am using only disappears when i hit something else.

Any idea to how to solve this? I must be missing something basic here, but as i said i don't know much about C# yet. alt text

airshoot-min.png (444.3 kB)
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 doublemax · Nov 06, 2016 at 09:21 PM

 if (target = true)

should be

 if (target == true)

The first one is an assignment with will always evaluate to "true",

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 06, 2016 at 10:00 PM -1
Share

Edit: Never$$anonymous$$d, after playing 6 times somehow it fixed itself and now everything is working fine. Thanks, i didn't know that i needed to use two equals == =)

avatar image Zitoox · Nov 06, 2016 at 10:18 PM -1
Share

I was wrong =\

I figured out that i can STILL even if i am NOT ai$$anonymous$$g at the enemy, shoot and i will receive the "Target Hit!" log. Before editing the script, no matter how many times i shot the air, it registered. Now it only registers for the first time.

This means that if i click for the first time in anything, the log will be displayed. But the following shoots will not happen and the log will not be displayed even if i shoot the Enemy.

But if i don't shoot anything until i am ai$$anonymous$$g at the enemy and then shoot, it will work. How can i fix that?

avatar image doublemax Zitoox · Nov 06, 2016 at 10:26 PM 0
Share

Right now the firing does not depend on the "target" variable.

 void Update()
 {
   nextFire -= Time.deltaTime;
 
   if (Input.GetButton("Fire1") && target && nextFire <= 0)
   {
     Flash.SetActive(true);
     Debug.Log("Target Hit!");
     nextFire = fireRate;
   }
 }
avatar image Zitoox doublemax · Nov 06, 2016 at 11:06 PM -1
Share

Now i get it, it's working! Thank you kindly.

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

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

Related Questions

Detect tag in C# 1 Answer

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can i convert float to int ? 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