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 /
This question was closed Sep 11, 2016 at 01:26 AM by jacupiri for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by jacupiri · Jul 27, 2016 at 07:29 PM · scripting problembug-perhapsartificial intelligence

Why my NPCs shoot the player but not other GameObjects?

Hi folks! I've been studying Unity and C# for a while now, but I'm having a hard headache with this problem. It looks like a bug, but it can be a scripting error (AKA myfault).

Can anyone figure out why, using this script, my NPCs do shoot at the Player when I type "Player" on targetTag, but don't shoot other NPCs? Even if I change the NPCs tag to Player, they won't do it!

If I wanted them to shoot the player, the script would be just fine: the only shoot when they have a clear line of sight and on a random basis...

using UnityEngine; using System.Collections;

public class FireRangedWeapon : MonoBehaviour {

 [SerializeField] private string targetTag;
 [SerializeField] private float power = 10.0f;
 [SerializeField] private float weaponRange = 20f;
 [SerializeField] private int minReloadTime;
 [SerializeField] private int maxReloadTime;

 private float reloadTime;
 private GameObject target;

 public GameObject projectile;


 void Start ()

 {
     StartCoroutine (Attack ());
 }


 IEnumerator Attack()
 {
     FindClosest ();
     if ((Vector3.Distance (transform.position, target.transform.position) <= weaponRange) && !Physics.Linecast(transform.position,target.transform.position,1))
     {
         transform.LookAt (target.transform);
         GameObject newProjectile = Instantiate(projectile, transform.position + transform.forward, transform.rotation) as GameObject;
         newProjectile.GetComponent<Rigidbody>().AddForce(transform.forward * power, ForceMode.VelocityChange);
     }

     reloadTime = Random.Range (minReloadTime, maxReloadTime);
     yield return new WaitForSeconds (reloadTime);
     StartCoroutine (Attack ());
 }
     
 void FindClosest()
 {
     GameObject[] gos;
     gos = GameObject.FindGameObjectsWithTag(targetTag);
     GameObject closest = null;
     float distance = Mathf.Infinity;
     Vector3 position = transform.position;
     foreach (GameObject go in gos) {
         Vector3 diff = go.transform.position - position;
         float curDistance = diff.sqrMagnitude;
         if (curDistance < distance) {
             closest = go;
             distance = curDistance;
         }
     }
     target = closest;
 }

}

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
1
Best Answer

Answer by jacupiri · Aug 06, 2016 at 11:35 PM

Even though no one answered my question, I found out by myself that it was a matter of Layers, because I was using a Linecast as part of one of the "if" statements. Since the gameObjects were on the same layer where the Linecast was looking for obstacles, they never shot each other...

So I just set the NPCs to another layer and that was the end of it!

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

Follow this Question

Answers Answers and Comments

76 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

Related Questions

Changing script variable from another script doesn't change it in the original script? 0 Answers

Can jump even when in the air. 0 Answers

OnTriggerEnter2D tiggers twice 0 Answers

AI Waypoint does not loop 1 Answer

Why is Unity showing the wrong variables in the Inspector? 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