Hello, Currently I have successfully implemented an enemy that can moving in a circular motion, however, when I begin the game the enemy goes off to a completely random area of the level. How would I ensure that the enemy stays within a specific area while moving circular? Thank you.
using UnityEngine;
using System.Collections;
public class CircularMovement : MonoBehaviour {
//timer
float timeCounter = 0;
//speed
public float speed;
public float width;
public float height;
// Use this for initialization
private void Start () {
speed = 2;
width = 2;
height = 3;
//start in positon
transform.position = new Vector3(53.939f, 1.201f);
transform.position = transform.position;
}
// Update is called once per frame
void Update () {
timeCounter += Time.deltaTime*speed;
//directional path
float x = Mathf.Cos (timeCounter)*width;
float y = Mathf.Sin (timeCounter)*height;
transform.position = new Vector2(x, y);
}
}
↧