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 /
  • Help Room /
This question was closed Sep 11, 2016 at 02:47 PM by ygh-jk for the following reason:

Figured it out myself :L

avatar image
0
Question by ygh-jk · Sep 07, 2016 at 06:24 AM · charactermovepause

NPC still moves even when paused.

update: pause code added

As per title, if the game is paused, everything stops, even the NPC pauses when patrolling. However, when the NPC goes from idle and patrol to chase mode, it goes after the player. When paused though during chase mode, the animations pause but the NPC still moves slowly, which I noticed the X and Z position increases. I'm assuming it has to do with the NPC's transform but after adding and removing bits, still no idea why.

Heres the coding: using UnityEngine; using System.Collections;

 public class CHASE : MonoBehaviour 
 {
     public Transform player;
     public Transform myTransform;
     static Animator anim;
     public float moveSpeed = 3.0f;
     public Transform[] points;
     private int destPoint = 0;
     private NavMeshAgent agent;
 
 
     void Start () 
     {
         anim = GetComponent<Animator> ();
         agent = GetComponent<NavMeshAgent>();
         agent = GetComponent<NavMeshAgent>();
         GotoNextPoint();
     }
 
 
 
     void Update () 
     {
         Vector3 direction = player.position - this.transform.position;
         direction.y = 0;
         float angle = Vector3.Angle (direction, this.transform.forward);
         if (Vector3.Distance (player.position, this.transform.position) < 30 && angle < 180) 
         {
 
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
             this.transform.rotation = Quaternion.Slerp 
                 (this.transform.rotation, Quaternion.LookRotation (direction) , 0.1f * Time.deltaTime * 50.0f) ;
 
                 anim.SetBool ("isIdle", false);
                 if (direction.magnitude > 5) 
             {        
                     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
                     this.transform.rotation = Quaternion.Slerp 
                     (this.transform.rotation, Quaternion.LookRotation (direction) , 0.1f * Time.deltaTime * 50.0f) ;
                 this.transform.Translate (0f * Time.deltaTime, 0f * Time.deltaTime, 0.05f);
                     anim.SetBool ("isChasing", true);
                     anim.SetBool ("isDeath", false);
                 } else {
                     anim.SetBool ("isChasing", false);
                     anim.SetBool ("isDeath", false);
                     }
         }else if(agent.remainingDistance < 0.5f)
                     {
                     anim.SetBool("isIdle", true);
                     anim.SetBool("isChasing", false);
                     anim.SetBool("isDeath", false);
                     GotoNextPoint();
                     }
 
 
     }
 
     void GotoNextPoint() 
     {
         // Returns if no points have been set up
         if (points.Length == 0)
             return;
         Debug.Log ("Not chasing", gameObject);
 
         // Set the agent to go to the currently selected destination.
         agent.destination = points[destPoint].position;
 
         // Choose the next point in the array as the destination,
         // cycling to the start if necessary.
         destPoint = (destPoint + 1) % points.Length;
     }
 
 }
 

Heres also the pause script

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class Pause_menu : MonoBehaviour 
 {
 
     GameObject[] pauseObjects;
 
     // Use this for initialization
     void Start () 
     {
         Time.timeScale = 1;
         pauseObjects = GameObject.FindGameObjectsWithTag("ShowOnPause");
         hidePaused();
     }
 
     // Update is called once per frame
     void Update () 
     {
 
  
         if(Input.GetKeyDown(KeyCode.Escape))
         {
             if(Time.timeScale == 1)
             {
                 Time.timeScale = 0;
                 showPaused();
             } else if (Time.timeScale == 0){
                 Debug.Log ("high");
                 Time.timeScale = 1;
                 hidePaused();
             }
         }
     }
 
 
     //Reloads the Level
     public void Reload()
     {
         SceneManager.LoadScene(3);
     }
 
     public void Reload_MM()
     {
         SceneManager.LoadScene(("Main menu"));
     }
 
     //controls the pausing of the scene
     public void pauseControl()
     {
         if(Time.timeScale == 1)
         {
             Time.timeScale = 0;
             showPaused();
         } else if (Time.timeScale == 0)
         {
             Time.timeScale = 1;
             hidePaused();
         }
     }
 
     //shows objects with ShowOnPause tag
     public void showPaused()
     {
         foreach(GameObject g in pauseObjects)
         {
             g.SetActive(true);
         }
     }
 
     //hides objects with ShowOnPause tag
     public void hidePaused()
     {
         foreach(GameObject g in pauseObjects)
         {
             g.SetActive(false);
         }
     }
 
     //loads inputted level
     public void ExitGame()
     {
         Application.Quit();
     }
 }
 
Comment
Add comment · Show 1
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 ygh-jk · Sep 11, 2016 at 02:47 PM 0
Share

O$$anonymous$$FG, I figured it out myself after messing about. So to fix this, all I had to code was

 this.transform.Translate (0f , 0f , 0.05f);

to

 this.transform.Translate (0f , 0f , 0.05f* Time.deltaTime);

or just turn 0.05f to 0.

God damn, Im stupid.

1 Reply

  • Sort: 
avatar image
0

Answer by Jessespike · Sep 08, 2016 at 06:05 PM

You mention pausing but don't describe how it's done. I'm guessing it's done via Time.timeScale ??? If so, you need to update based off that as well.

example:

 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

should be:

 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime * Time.timeScale;
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 ygh-jk · Sep 09, 2016 at 01:43 AM 0
Share

Added the pause script to the question. So I added Time.timeScale but the AI that chases the player still doesnt stop even when paused. The biggest clue to this see$$anonymous$$gly is that the Position X and Z in the Transform still increases/decreases values until it stops when near the player.

Follow this Question

Answers Answers and Comments

79 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make pivot point follow object in animation? 1 Answer

Pause scene when character hit object, then resume it from another scene 0 Answers

i can't move anything , and nothing in Assest > Import Package 0 Answers

Hi ,I'd like to know , is there a tool that is available that will allow for a player in game to create their own character just like in Sims4 the game ? 0 Answers

Character won't rotate along with camera on y axis 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