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 BIO_K · Sep 25, 2017 at 11:39 AM · transformvector3navmeshfunctionnavmeshagent

Having some problem with AI finding a vector3...

Okay so this script works pretty good the only problem is my ally will not find their StartPos. take a look, basicly the state that tells them that they are home is not activated as they cannot find the correct transform... Please just read the script tell me what you think and i should do... And why myTransform does not equal StartPos when they are at the startPOS.

using UnityEngine; using UnityEngine.AI;

public class AllyAI : MonoBehaviour { //Transforms public Transform Target; public int rotSpeed; public int maxDistance;

 private Transform myTransform;

 private Vector3 NPCstartPos;
 private Quaternion NPCstartRot;

 //Essenial Compnents
 private Animator anim;

 //NavMesh Components
 Transform _destination;
 NavMeshAgent _nvAgent;

 //States
 public bool NPC1IdleState;
 public bool NPC2HomeState;
 public bool NPC3AttackState;
 public bool NPC4ReturnHomeState;

 void Awake() {
    myTransform = transform;
     NPCstartPos = transform.position;
     NPCstartRot = transform.rotation;

     //Set States
     NPC1IdleState = true;
     NPC2HomeState = true;
     NPC3AttackState = false;
     NPC4ReturnHomeState = false;
 }

 // Use this for initialization
 void Start () {
       maxDistance = 1;

     //Get Components
     anim = GetComponent<Animator>();
     _nvAgent = this.GetComponent<NavMeshAgent>();

     //Set NavMeshFalse
     gameObject.GetComponent<NavMeshAgent>().enabled = false;
 }
 
 // Update is called once per frame
 void Update () {
     //DEBUG
     Debug.DrawLine(Target.position, myTransform.position, Color.cyan);
     
     Vector3 direction = Target.position - this.transform.position;
  
     //Attack State
     if (Vector3.Distance(Target.position, myTransform.position) > maxDistance && Vector3.Distance(Target.position, myTransform.position) < 20)
     {
         //Look at Target
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(Target.position - myTransform.position), rotSpeed * Time.deltaTime);
         //Enable NavMesh
         gameObject.GetComponent<NavMeshAgent>().enabled = true;
         //Move to Target      
         anim.SetTrigger("isWalking");
         _destination = Target;
         Vector3 targetVector = _destination.transform.position;
         _nvAgent.SetDestination(targetVector);
         //Set State
         NPC1IdleState = false;
         NPC2HomeState = false;
         NPC3AttackState = true;
         NPC4ReturnHomeState = false;
     }

     //Idle State
     else if (Vector3.Distance(Target.position, myTransform.position) < maxDistance) {
         anim.SetTrigger("isIdle");
         _destination = null;
         gameObject.GetComponent<NavMeshAgent>().enabled = false;
         //Set State
         NPC1IdleState = true;
         NPC2HomeState = false;
         NPC3AttackState = false;
         NPC4ReturnHomeState = false;
     }

     //Target Out Of Sight
     if (Vector3.Distance(Target.position, myTransform.position) > 20)
     {
         //Check To Return Home
         if (myTransform.position != NPCstartPos && myTransform.rotation != NPCstartRot)
         {   //Set States
             NPC1IdleState = false;
             NPC2HomeState = false;
             NPC3AttackState = false;
             NPC4ReturnHomeState = true;

             //DEBUG
             Debug.Log("Ally Cannot Find Home");

              if (myTransform.position == NPCstartPos && myTransform.rotation == NPCstartRot)
             {
                 //Set States
                 NPC1IdleState = false;
                 NPC2HomeState = true;
                 NPC3AttackState = false;
                 NPC4ReturnHomeState = false;

                 //DEBUG
                 Debug.Log("Ally Found Home");
             }
         }      
     }

     //Return To Home State
     if (NPC4ReturnHomeState == true)
     {
         //Enable NavMesh
         gameObject.GetComponent<NavMeshAgent>().enabled = true;
         //Set NavMesh Variables
         _destination.position = NPCstartPos;
         Vector3 targetVector = _destination.position;
         _nvAgent.SetDestination(targetVector);
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(_destination.position - myTransform.position), rotSpeed * Time.deltaTime);
         //Set Animation
         anim.SetTrigger("isWalking");     
         
     }

     //At Home
     else if (NPC2HomeState == true)
     {
         _destination = null;
         //Disable NavMesh
         gameObject.GetComponent<NavMeshAgent>().enabled = false;
         //Set Animation
         anim.SetTrigger("isIdle");
         //Set States
         NPC1IdleState = true;
         NPC2HomeState = true;
         NPC3AttackState = false;
         NPC4ReturnHomeState = false;
     }
 }

}

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

144 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

Related Questions

How to make NavMesh agent move directly forward 0 Answers

MoveTowards instantly repositioning object to target position 2 Answers

Is there a way to make a follow script without NavMesh? 1 Answer

Question about Vector creation 1 Answer

Applying direction into transform position 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