- Home /
 
               Question by 
               qanqa · Jun 10, 2017 at 08:00 AM · 
                movementscripting beginnerpathfindingcharacter controllertargetting  
              
 
              Merging pathfinding and following the player Scripts
Hi, I'm creating my first game in Unity which is 3D Pacman Clone. I created maps etc. but I stuck with ghost movement. It too difficult for me to duplicate Pacman ghost's behaviour for now but I got two scripts. First one is just making character moving randomly and second one is making character chase the Player when Player is in characters range but when it's not character just stops. I tried to interconnect these two scripts into one. It should make character move randomly. When Player is in characters range it should chase but then when Player will be out of range it should again move randomly.
I really appreciate any help!
Chasing script :
     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class EnemyMovementChaseConverted : MonoBehaviour {
     
         Transform target; //the enemy's target
         public float moveSpeed = 0f; //move speed
         public float rotationSpeed = 0f; //speed of turning
         public float range = 0f; //Range within target will be detected
         public float stop = 0f;
         Transform myTransform; //current transform data of this enemy
         void Awake()
         {
             target = GameObject.FindWithTag("Pacman").transform; //target the player
             myTransform = transform; //cache transform data for easy access/preformance
         }
         void Update()
         {    //rotate to look at the player
             float distance = Vector3.Distance(myTransform.position, target.position);
             if (distance <= range)
             {
                 //look
                 myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
                 Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
                 //move
                 if (distance > stop)
                 {
                     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
                 }
             }
         }
     }
 
 
Move randomly script:
 using UnityEngine.AI;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveRandomly : MonoBehaviour {
 
     public float timer;
     public int newtarget;
     public float speed;
     public UnityEngine.AI.NavMeshAgent nav;
     public Vector3 Target;
 
 
     // Use this for initialization
     void Start () {
 
         nav = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
         
     }
     
     // Update is called once per frame
     void Update () {
 
         timer += Time.deltaTime;
 
         if(timer >= newtarget)
         {
             newTarget();
             timer = 0;
             nav.speed = speed;
         }
     }
 
     void newTarget()
     {
         float myX = gameObject.transform.position.x;
         float myZ = gameObject.transform.position.z;
 
         float xPos = myX + Random.Range(myX - 100, myX + 100);
         float zPos = myZ + Random.Range(myZ - 100, myZ + 100);
 
         Target = new Vector3(xPos, gameObject.transform.position.y, zPos);
 
         nav.SetDestination(Target);
     }
 }
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                