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 Azaroth · Jan 16, 2014 at 06:43 PM · aistuckmonster

Monster AI

Hey guys, this is the code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MonsterAI : MonoBehaviour {
 
     public int i;
 
     public List<Vector3> Targets = new List<Vector3>();
     
     RaycastHit hit;
     public bool on;
     
     public float TurnSpeed;
     public float Speed;
     
     CharacterController controller;
     
     // Use this for initialization
     void Start () {
         
         controller = GetComponent<CharacterController>();
 
         gameObject.SendMessage("Old");
 
     }
     
     void OnTriggerEnter(){
         
         on = true;
 
         i = Targets.Count-1;
 
     }
     
     // Update is called once per frame
     void Update () {
         
         if(on){
 
             if(transform.position == Targets[i]){
                 i++;
             }
 
             // find the target direction:
             Vector3 dir = Targets[i] - transform.position;
             
             dir.y = 0; // make direction strictly horizontal
             dir.Normalize(); // normalize it
             
             // turn gradually to the target each frame:
             transform.forward = Vector3.Slerp(transform.forward, dir, TurnSpeed * Time.deltaTime);
             
             Vector3 forward = transform.TransformDirection(Vector3.forward);
             controller.SimpleMove(forward * Speed);
             
         }
         
     }
     
     public IEnumerator Old(){
         
         yield return new WaitForSeconds(2);
         
         Targets.Add(GameObject.FindWithTag("Player").transform.position);
 
         gameObject.SendMessage("Old");
         
     }
     
 }
 

I want the monster to follow the steps of the player but it reaches near the first point and stucks, i dont know why.

Comment
Add comment · Show 3
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 axCed · Jan 16, 2014 at 11:16 PM 0
Share

Ins$$anonymous$$d of using bool on, you can use setactive to chose if the script is working or not.

avatar image axCed · Jan 16, 2014 at 11:26 PM 0
Share

What I like to do and I suggest to you is to use a collider to see if the point is contained. You need less accuracy with the movement to reach the point. Also, if you have a lot of points very close to each other in many different orientation, it will prevent the gameObject to "go crazy all around" and turning on itself.

avatar image Azaroth axCed · Jan 17, 2014 at 04:13 PM 0
Share

Thank you, the problem it the end was the ground. I straigthened it and it works fine. But many maps are not straight so i need to fix it. But i cant change the collider material on the character controller. Any suggestions?

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

18 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

Related Questions

AI Monster Problem 3 Answers

Wandering AI Monster 2 Answers

How to make a giant walking monster 1 Answer

How to check if a character hasn't changed its position in the last x seconds? 1 Answer

Enemy AI scripting help 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