I need some help with my bad guy class. I’m trying to learn a simple vector movement. Pick a vector from bad guy to player, and move halfway there. Then another point will be picked, and it will move there. I’ll make something else happen when it gets close.
This is all just to learn stuff. Not sure what’s going on. My vector x and y isn’t getting cut in half, and the bad guy only moves once.
using UnityEngine;
using System.Collections;
public class Bad : MonoBehaviour {
public Transform player;
private bool playerDetected;
private Vector3 destinationPoint;
private bool moveInitiated;
private bool moveReached;
private float distanceFromPlayer;
// player rotation
private Vector3 zAxis = new Vector3(0, 0, 1);
// Use this for initialization
void Start () {
playerDetected = false;
moveInitiated = false;
moveReached = false;
}
// Update is called once per frame
void Update () {
if (playerDetected) {
transform.RotateAround(player.transform.position, zAxis, 0.5f);
} else {
if(!moveInitiated && !playerDetected)
{
set_destination();
moveInitiated = true;
}
if(moveInitiated && !playerDetected)
{
if(!moveReached)
{
move();
}
if(return_lerp_distance() <= 0.04)
{
moveInitiated = false;
moveReached = false;
Debug.Log("Move Complete");
}
}
}
// see if player is close enough to detect to go into player detected logic
if(get_distance() <= 1.0f)
{
playerDetected = true;
}else{
playerDetected = false;
}
// end update
}
void set_destination()
{
Vector3 destinationPoint = player.transform.position - transform.position;
destinationPoint.x = destinationPoint.x * 0.5f;
destinationPoint.y = destinationPoint.y * 0.5f;
}
void move()
{
transform.position = Vector3.Lerp(transform.position,destinationPoint, Time.deltaTime / 3.0f);
}
float get_distance()
{
distanceFromPlayer = Vector3.Distance (player.transform.position, transform.position);
return distanceFromPlayer;
}
float return_lerp_distance()
{
return Vector3.Distance (transform.position, destinationPoint);
}
}