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 /
avatar image
0
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
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Isometric Movement Path (8 Directions) 2 Answers

UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers

How to stop an object at a specific position for certain time in it's motion and then let continue it's motion? 0 Answers

character controller move, strange behaviour 0 Answers

Characters contending for same position 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