Toggling AI Follow
Hi folks,
So apologies in advance for what i'm sure is a very simple question.
I've not long started playing around in Unity, and have made an object that activates when the player comes close and follows them using help from this forum. I've then added a toggle to turn on isKinematic for the rigidbody on the object to give it a floating / solid state switch (like ghost mode/physical mode), and what I want to do now is make a toggle that can turn on whether the object follows you or not.
So ideally it would be something like "Press E to make the object follow or stop".
I've tried a fair few variations on putting it together but my limited knowledge is hampering me, so any advice would be great.
Here's my current code:
using UnityEngine;
using System.Collections;
public class EnemyAi : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
int maxDistance;
int minDistance;
private Transform myTransform;
void Awake()
{
myTransform = transform;
}
void Start ()
{
maxDistance = 2;
minDistance = 4;
}
void Update ()
{
if (Vector3.Distance (target.position, myTransform.position) > minDistance) {
Vector3 dir = target.position - myTransform.position;
// Normalize it so that it's a unit direction vector
dir.Normalize();
}
else if (Vector3.Distance(target.position, myTransform.position) > maxDistance)
{
Vector3 dir = target.position - myTransform.position;
dir.Normalize();
myTransform.position += dir * moveSpeed * Time.deltaTime;
}
if(Input.GetKeyDown(KeyCode.R))
rigidbody2D.isKinematic = true;
if(Input.GetKeyDown(KeyCode.F))
rigidbody2D.isKinematic = false;
}
}
Sorry again for the potentially stupid question, like I said i'm really new to all this.
Thanks in advance for all your help!
Your answer
Follow this Question
Related Questions
How can I program a select level Map like the Super mario bro 3 or Shovel Knight? 0 Answers
Need Help Getting Basic Mechanics Started (2D Puzzle Platformer) 0 Answers
How to stop an object from following its parent? 3 Answers
"Invalid argument"? 0 Answers
How to fix spite character changing position when turning? 1 Answer