Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Therian13 · Jul 12, 2014 at 11:03 PM · lookatfindgameobjectswithtagassign

AI cannot find closest Target

Hello Everyone, I have a fox that attacks humans when they are too close, but the problem is, it will only attack the one gameobject I assign it too. I want the game to be multiplayer, so I want it to attack anything with the tag "Player" when it gets too close. The script I made seems to have problems assigning the closest target. Could anyone explain to me what I am doing wrong. I greatly appreciate your help.

Here is my script.

 var Target : Transform;
 var Distance;
 
 var LookatDistance = 25.0;
 var ChaseRange = 15.0;
 var AttackRange = 2.0;
 var MoveSpeed = 5.0;
 var Damping = 6.0;
 var AttackRepeatTime = 1;
 
 
 var moveSpeedCalm = 0.2;
 var ChaseRangeCalm = 0.5;
 var LookatDistanceCalm = 0.7;
 
 var isAngry: foxWalkScript;
 
 var MonsterAttack = 25;
 
 private var AttackTime : float;
 
 var controller : CharacterController;
 var Gravity  : float = 20.0;
 private var MoveDirection : Vector3 = Vector3.zero;
 
 
 
 
 function Start ()
 {
 
     AttackTime = Time.time;
     
     
 }
 
 function Update ()
 
 {
     
         Distance = Vector3.Distance(Target.position, transform.position);
         
         if (Distance > LookatDistance)
         {
 
             isAngry.provoked =false;
         }
         if (Distance < LookatDistance)
         {
 
             LookAt();
         }
         
         
         if (Distance < AttackRange)
         {
             Attack ();
             isAngry.provoked =true;
         }
          
         else if (Distance < ChaseRange) 
         {
             chase ();
             isAngry.provoked =true;
         }
         
         
                    //////////////////
              var direction = transform.TransformDirection(Vector3.forward);
             var hit : RaycastHit;
  
            if (Physics.Raycast (transform.position, direction, hit, 20)) 
                {
            Debug.DrawLine (transform.position, hit.point, Color.yellow);
             }
             /////////////////        
     
 }
 
 
 function LookAt ()
 {
 
     Target =  GameObject.FindGameObjectWithTag("player").transform;
     yield WaitForSeconds(0.1);
     var rotation = Quaternion.LookRotation(Target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }
 
 function chase ()
 {
 
     moveDirection = transform.forward;
     moveDirection *= MoveSpeed;
     
     moveDirection.y -= Gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
      
 }
 
 function Attack ()
 {
     if (Time.time > AttackTime)
     {
         Target.SendMessage("ApplyDamage", MonsterAttack );
         Debug.Log ("Insert Attack Here");
         AttackTime = Time.time + AttackRepeatTime;
     }
     
 }
 
 function ApplyDamage ()
 {
 if (isAngry.provoked == true)
     {
     ChaseRange = 4;
     MoveSpeed = 2;
     LookatDistance = 4;
     }
 }
 
 

Its suppose to assign the target at the LookAt function, but doesn't want to...

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Is FindGameObjectWithTag() too heavy on resources for 50 objects? 0 Answers

How to import the object from server to unity 2 Answers

Material doesn't have a color property '_Color' 4 Answers

Setting Scroll View Width GUILayout 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