• 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
1
Question by serkank · Jan 29, 2012 at 08:16 PM · terrainai

AI Script walking on terrain

i was looked Some script in internet But all have problem while my enemy model Walk on Terrain..

i am triying use Character Controller and Character Motor But my character Turn Face To Y axiss if target point y axis value diffrent.

How i can write script ignore target y axis?

This is my code ..

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     public Transform target;
     public int moveSpeed;
     private Vector3 centerPoint;
     private float distance;
     private CharacterController character;    
     private Vector3 targetpoint ;
     private Vector3 moveDirection;
     private int gnp = 0; 
     
     void Awake(){
         centerPoint = transform.position;
     }
     
 
     void Start () {
          character = GetComponent<CharacterController>();
     }
 
     void Update () {    
         Walk();
     }
 
     void GotoTargetPoint(){
 
             animation.CrossFade ("walk");
             Vector3 moveDirection  = targetpoint - transform.position;    
         
             if (moveDirection.magnitude < 2){
                 switch(gnp){
                     case 10:
                         gnp = 1;
                         Debug.Log("GNP " + gnp);
                     break;
                 
                     case 20 :
                         gnp=0;
                         Debug.Log("GNP " + gnp);
                     break;
                 }
                 
             }else{
                 transform.LookAt(targetpoint);
                 character.Move(moveDirection.normalized * moveSpeed * Time.deltaTime);    
             }
     }
     
     
     void GetNewPoint(){
         targetpoint =  new Vector3(Random.Range(-13.0F, 13.0F), 0, Random.Range(-13.0F, 13.0F));
         targetpoint = targetpoint + transform.position;
         targetpoint.y = Terrain.activeTerrain.SampleHeight(targetpoint);
         gnp = 10;            
     }
     void Walk(){
         if (gnp == 0){
             GetNewPoint();
             Debug.Log("GNP " + gnp);
         }
         if (gnp == 1 ){
             targetpoint=centerPoint;
             gnp = 20;
             Debug.Log("GNP " + gnp);
         }    
         GotoTargetPoint();
     }
         
 }

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jan 29, 2012 at 09:07 PM

Copy targetpoint to a variable flatTarget, and copy the player's Y coordinate to it, so that flatTarget and the player will be at the same plane:

... void GotoTargetPoint(){

 animation.CrossFade ("walk");
 // create a "flat" targetpoint copying the player's y coordinate:
 Vector3 flatTarget = targetpoint;
 flatTarget.y = transform.position.y;
 Vector3 moveDirection = flatTarget - transform.position; // use flatTarget instead of targetpoint   
 if (moveDirection.magnitude < 2){
     switch(gnp){
         case 10:
             gnp = 1;
             Debug.Log("GNP " + gnp);
         break;
         case 20 :
             gnp=0;
             Debug.Log("GNP " + gnp);
         break;
     }
 }else{
     transform.LookAt(flatTarget); // <- look at the flatTarget point
     character.Move(moveDirection.normalized * moveSpeed * Time.deltaTime);  
 }

}

Comment
Add comment · 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 serkank · Jan 30, 2012 at 08:09 PM 0
Share

:) Thank you.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Get Height of Terrain in Script 1 Answer

ExportTerrain doesn't work 1 Answer

Start another thread for a function 1 Answer

Waypoint Setup: How? 4 Answers

Enemy behavior is not working after the enemy has spawned. 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