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
}
Answer by TBruce · Apr 29, 2016 at 07:43 PM
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;
}
}
@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 :-/
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
}
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!