• 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 Jan 06, 2017 at 08:57 PM by awsome129 for the following reason:

The question is answered, right answer was accepted

avatar image
Question by awsome129 · Jan 06, 2017 at 04:58 PM · airandompathfindingrandom.rangepatrol

Can no1 help me???

I have posted this question twice and even with multiple advice, it has not helped.

How do i make ai move between random points(I have this working), but have them move there and not teleport. O have tried lerp and nothing. Help please!!

Script(mainly handling the ai move under if (waypointtimer <= 0):

 public class AIBehaviour : MonoBehaviour
 {
 
     public NavMeshAgent agent;
     public Transform player;
     private float DAI;
     bool PlayerSeen;
     float movespeed;
     float timer = 5;
     private Vector3 newPos;
     public int moveCount;
     public bool AImove;
     Animator anim;
     bool AIpatrol;
     float AIhealth;
     Vector3 randomPos;
     float waypointtimer = 5;
     GameObject prefab;
     GameObject Waypoint;
 
 
 
     // Use this for initialization
     void Start()
     {
         PlayerSeen = false;
         movespeed = 1;
         moveCount = 0;
         AImove = false;
         anim = GetComponentInChildren<Animator>();
         AIhealth = 100;
         newPos = transform.position;
         prefab = Resources.Load("Waypoint") as GameObject;
     }
     // Update is called once per frame
     void Update()
     {
 
         timer -= Time.deltaTime;
 
         var aix = new Vector3(Random.Range(0, 20), 0, Random.Range(0, 20));
 
         Waypoint = Instantiate(prefab, aix, Quaternion.identity) as GameObject;
 
         if (timer < 0)
         {
             timer = 5;
         }
 
         waypointtimer -= Time.deltaTime;
 
         Vector3 positionA = new Vector3(Random.Range(0, 50), 0, Random.Range(0, 20));
         if (waypointtimer <= 0)
         {
             newPos = positionA;
             Vector3 positionB = new Vector3(Random.Range(0, 50), 0, Random.Range(0, 20));
             transform.position = Vector3.Lerp(positionA, positionB, Time.deltaTime * movespeed);
             waypointtimer = 20;
         }
 
         Vector3 direction = player.position - this.transform.position;
         float angle = Vector3.Angle(direction, this.transform.forward);
         float angleb = -Vector3.Angle(direction, this.transform.forward);
 
         if (Vector3.Distance(player.position, this.transform.position) < 10 && angle < 30 || (Vector3.Distance(player.position, this.transform.position) < 4 && angleb < 0))
         {
             PlayerSeen = true;
         }
 
 
         /* if (PlayerSeen == false)
            {
              transform.position = new Vector3(aix, 0.0f, aiz) * (movespeed);
             moveCount += 1;
            anim.SetBool("Take 001", AIpatrol);
             randomPos = transform.position;
            }
       }
        else
        {
 
            if (Vector3.Distance(player.position, this.transform.position) < 10 && angle < 30 || (Vector3.Distance(player.position, this.transform.position) < 4 && angleb < 0))
            {
              / direction.y = 0;
 
                this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                                            Quaternion.LookRotation(direction), 0.1f);
 
                if (direction.magnitude > 5)
                {
                    this.transform.Translate(0, 0, 0.3f);
                }
            }
            *
            */
         { 
             if (moveCount == 2)
             {
                 AImove = true;
             }
 
             if (moveCount == 2)
             {
                 AImove = false;
             }
 
             if (Vector3.Distance(player.position, this.transform.position) > 10 || (Vector3.Distance(player.position, this.transform.position) > 4))
             {
                 PlayerSeen = false;
             }
 
         }
     }
 }
 
     
 
Comment

People who like this

0 Show 2
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 Owen-Reynolds · Jan 06, 2017 at 04:57 PM 0
Share

Have people been suggesting various things, that you sort of understand, and you've been cramming them all into this? That's always a mess.

My advice is to restart with what you know. Something simple that you can test. For example, walking toward one point, which you set in the Inspector. Then think of one think to add - like making that one target point be random. Then test that. Maybe something someone wrote earlier will help, or not. Maybe most the stuff in the script you have now will eventually make sense. Just keep adding (and testing) things which you understand.

If just the programming part is trouble, it's faster and less frustrating to read a C# book. I (obviously) think mine at taxesforcatses is the best, but you should be able to get other recommendations here (but not the Unity/Learn section - you wouldn't learn Photoshop from the Unity site, and neither should you learn programming.))

avatar image awsome129 Owen-Reynolds · Jan 06, 2017 at 08:57 PM 0
Share

i will look into your book ty

1 Reply

  • Sort: 
avatar image

Answer by joelunity · Jan 06, 2017 at 07:20 PM

I beleive this code will work for you

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class randomMove : MonoBehaviour { float movespeed = .08f; Vector3 aix = new Vector3(0, 0, 0); void Update () {

     if (transform.localPosition != aix)
     {
         transform.LookAt(aix);
         transform.position = Vector3.MoveTowards(transform.position, aix, movespeed);
     }
     else
     {
         aix = new Vector3(Random.Range(0, 20), 0, Random.Range(0, 20));
     }

 }

}

Comment

People who like this

0 Show 1 · 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 awsome129 · Jan 06, 2017 at 08:55 PM 0
Share

ty so much that worked so good!!!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning object with new random pathing.,Randomly moving after spawn 0 Answers

How can I let my car AI follow a Node-Path slightly random? (C#) 0 Answers

Randomly Spawn Outside The Camera Field of View 1 Answer

Use all variables from array (in Random.Range()) 2 Answers

AI Patrol 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