looping forward/back of AI transform in relation to player position

Hi, I’m trying to make an enemy tank approach the player when it’s within a certain range and then reverse back to where it was and then approach the player’s position again and repeat in a loop until it’s destroyed or the player is out of range. I find I can get it back to it’s initial position easily enough using transform.position = tankPos; after I declare tankPos as a private vector3, but it’s sort of teleporting the enemy and it’s not what I want.
The problem is if I use transform.Translate (Vector3.back * moveSpeed * Time.deltaTime); the tank begins going backwards but then locks into a position because the

if (playerDistance < 80f) {
      
                        chase ();
                }

is now getting called!

If anyone can give me any advice about a technique that might work that would be great.

I’ve also tried using fixed joints and spring joints and the results weren’t so good.

using UnityEngine;
using System.Collections;

public class TankScript : MonoBehaviour {
   
    public Transform player;
    public float playerDistance;
    public float rotationDamping;
    public float moveSpeed;
    public static bool isPlayerAlive = true;
    //public float speed;
    public Rigidbody tankBull;
    public float speed = 20;
    public Transform spawnPoint;


    public float elapsedTime = 0.0f;
    public float shootRate = 3.0f;
   
    public float lastFireTime;
    public float fireDelay;
    private float time;
    GameObject MockronBossFour;
    float lockPos = 0;
    private Vector3 tankPos;


    void Start () {


        MockronBossFour = GameObject.Find ( "MockronBossFour" );
        MockronBossFour.transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, lockPos, transform.rotation.eulerAngles.z);
        tankPos = transform.position;
   

    }
   

    void Update () {
       
   


                if (isPlayerAlive) {
                        playerDistance = Vector3.Distance (player.position, transform.position);
           
                        if (playerDistance < 80f) {
                                lookAtPlayer ();
                        }
                }

                if (playerDistance < 80f) {
       
                        chase ();
                }

                if (playerDistance < 10f) {
           
                        back ();

                }
           
        }   
   

    void lookAtPlayer()
    {
        Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
        transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationDamping);
   
    }

   
    void chase ()
    {

                transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
   
        }

    void back (){

        transform.Translate (Vector3.back * moveSpeed * Time.deltaTime);
        transform.position = tankPos;
        }

You could add a bool variable to check if chase is “active” or not. If tank is going for the player, bool chasing = true and add
if ((playerDistance < 80f) && (chase != true)) and set chase = false if your player is out of range.
This way you need to set

chase = false;

when your tank is back in the position. The problem is, however, that your tank would always back the all way in and not start going towards player until it has reached tankPos again. I hope I understood what you mean and I hope this helps really =)

Hey thanks for your feedback.

After trying a lot of things I created a solution to get what I was after, but I feel the approach is clunky and maybe not the best approach.

I added two versions of the AI script to the tank, one which had chase();on it, and one withback();on it. and then I created triggers that if the tank ran into them would disable one code and enable the other, so now the tank goes back and forth whilst following the player.

There must be a more elegant way to do it. It seems like a pretty crude solution, but it kinda works.

having a deja vu moment… I’m fairly sure we’ve pointed you towards “finite state machine” answer for AI before?

http://wiki.unity3d.com/index.php?title=Finite_State_Machine

1 Like

Thanks, I’m sorry if I repeat myself sometimes.

Thankyou for your links.

These are the next things I’m going to try to learn for my next game levels: Nav mesh, A* pathfinding, Switch Statements, Enums, FSM, while loops.

1 Like