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 /
avatar image
0
Question by Paul2357 · Apr 29, 2016 at 06:40 PM · c#unity 5movementlocationtracking

Tracking transform position

So I have a script that takes the enemy and moves it between its starting position and the transform position of another hidden game object. The script works fine for the movement. However, I want the script to only run if the player isn't seen and then resume playing once the player is not seen once again. I have it tracking the location, but when I switch the bool, it jumps forward like the script was still working even though that code should not be running anymore due to the flag being set. I want it to stop and resume from the same place, but I'm hitting a wall at figuring it out. Code below:

 #region Notes Section
 /*
 
 Author: Paul Christopher
 Use: This code handles movement for the enemies
 Notes: Enemy moves back and forth at a random speed to a target point and back. (Cycle repeats)
 
 */
 #endregion
 
 using UnityEngine;
 using System;
 using System.Collections;
 
 public class EnemyMovement : Enemy {
 
     #region Variables
     //Movement Target
     public Transform farEnd;
 
     //Locations
     private Vector3 startLoc;
     private Vector3 targetLoc;
     private Vector3 currentLoc;
 
     //Time to complete one length
     System.Random rnd = new System.Random();
     public float secondsToComplete;
     #endregion
 
     #region Start
     // Use this for initialization
     void Start ()
     {
         //Movement
         startLoc = transform.position;
         targetLoc = farEnd.position;
 
         //Random
         secondsToComplete = rnd.Next(5, 15); //Sets random to be between 5 & 15 inclusive
     }
     #endregion
 
     #region Update
     // Update is called once per frame
     void Update ()
     {
         //Move if player is not seen
         if (!playerSeen)
         {
             StartCoroutine("Movement");
         }
         
         if (playerSeen)
         {
             StopCoroutine("Movement");
             transform.position = currentLoc;
         }
     }
     #endregion
 
 
     IEnumerator Movement()
     {
         //Moves enemy model between starting location (currentLoc) and a point (targetLoc). 
         //Model will continually move back and forth (as of right now)
         transform.position =
                 Vector3.Lerp(startLoc, targetLoc,
                 Mathf.SmoothStep(0f, 1f,
                 Mathf.PingPong(Time.time / secondsToComplete, 1f)));
 
         currentLoc = transform.position;
 
         yield return null;
     }
 
     //END OF FILE BELOW
 }
 
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 TBruce · Apr 29, 2016 at 07:43 PM

@Paul2357

Add this global

 private Coroutine movementCoroutine = null;
 

and the change Update() function to this

 void Update ()
 {
     //Move if player is not seen
     if (!playerSeen)
     {
         movementCoroutine = StartCoroutine(Movement());
     }
     else if (movementCoroutine != null)
     {
         StopCoroutine(movementCoroutine);
         movementCoroutine = null
         transform.position = currentLoc;
     }
 }
Comment
Add comment · Show 8 · 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 Paul2357 · Apr 30, 2016 at 12:26 AM 0
Share

@mavina the problem when I do that, the gameobject I have as a placeholder for the enemy launches into the air when playerSeen is true :-/

avatar image TBruce Paul2357 · Apr 30, 2016 at 07:53 PM 0
Share

@Paul2357

After rereading your question I gather what you are attempting to do is pause the Lerp when playerSeen is true. If this is the case you can do that with the following code

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Enemy$$anonymous$$ovement : $$anonymous$$onoBehaviour {
 
     #region Variables
     //$$anonymous$$ovement Target
     public Transform farEnd;
 
     //Locations
     public Vector3 startLoc;
 
     public Vector3 targetLoc;
     public Vector3 currentLoc;
 
     //Time to complete one length
     System.Random rnd = new System.Random();
     public float secondsToComplete;
     public bool playerSeen = false;
     public bool savePlayerSeen = false;
     float step;
     
     bool dirRight = true;
     float speed = 3;
 
     #endregion
 
     #region Start
     // Use this for initialization
     void Start ()
     {
         //$$anonymous$$ovement
         startLoc = transform.position;
         targetLoc = farEnd.position;
 
         //Random
         secondsToComplete = rnd.Next(5, 15); //Sets random to be between 5 & 15 inclusive
         savePlayerSeen = playerSeen;
 
         currentLoc = startLoc;
     }
     #endregion
 
     #region Update
     void Update ()
     {
         if (!playerSeen)
         {
             if (dirRight)
                 transform.Translate (Vector2.right * speed * Time.deltaTime);
             else
                 transform.Translate (-Vector2.right * speed * Time.deltaTime);
     
             if( transform.position.x >= targetLoc.x)
             {
                 dirRight = false;
             }
     
             if(transform.position.x <= startLoc.x)
             {
                 dirRight = true;
             }
         }
     }
     #endregion
 
     //END OF FILE BELOW
 }
avatar image Paul2357 TBruce · Apr 30, 2016 at 08:25 PM 0
Share

@mavina

That works as long as I freeze the Y position of my enemy. (which I don't think I'll need them moving on the Y axis so that shouldn't be a problem). Thanks!

Show more comments

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

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

Related Questions

How to stop movement script on void start and resume after. 0 Answers

Issues Getting my GameObject to both Move Forward and Jump properly. 0 Answers

How can I make it so that the longer you hold down the jump button the higher the player jumps? 0 Answers

Navigation in VR Mode?!! 0 Answers

Move the character automaticly with the road 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