I have my "enemy" following another "enemy", yet instead of following, it flies up in the air right off the bat and moves on all the axis.
public class BASICMOVEMENT : MonoBehaviour
{
Vector3 targetpos;
bool enemyseen;
object target;
public float movspeed = 1f;
public float step;
bool inAttackRange;
// Start is called before the first frame update
void Start()
{
enemyseen = false;
}
// Update is called once per frame
void FixedUpdate()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit RCC;
if (Physics.Raycast(transform.position, fwd, out RCC, 10)) {
if (RCC.collider.name == "hitbox")
{
enemyseen = true;
target = RCC.collider.transform.parent.gameObject;
targetpos = RCC.collider.transform.position;
Debug.Log(RCC.distance);
if (RCC.distance < 0.5)
{
inAttackRange = true;
}
}
}
}
void Update()
{
if (enemyseen == true)
{
/*StartCoroutine(Attack());*/
if (inAttackRange == false)
{
step = movspeed * Time.deltaTime;
this.transform.parent.gameObject.transform.position = Vector3.MoveTowards(transform.position, new Vector3(targetpos.x, transform.position.y, targetpos.z), step);
}
}
}
}
↧