Good day!
I experienced an unusual problem.After adding some left-right movement to an enemy(using velocity in FIxed Update),my Update function runs only once.For ex. if I add Debug.Log(“Hello”),it appears in console only in the beginning!Please tell me what is the problem.
public class FlyerEnemyMove : MonoBehaviour {
public float movingSpeed;
private int direction;
public float maxDist;
public float minDist;
// Use this for initialization
void Start ()
{
direction = -1;
maxDist += transform.position.x;
minDist -= transform.position.x;
}
void FixedUpdate()
{
movingLeftAndRight ();
}
void Update ()
{
Debug.Log("Hello");
}
void movingLeftAndRight()
{
switch (direction)
{
case -1:
{
// Moving Left
if( transform.position.x > minDist)
{
GetComponent <Rigidbody2D>().velocity = new Vector2(-movingSpeed,GetComponent<Rigidbody2D>().velocity.y);
}
else
{
direction = 1;
}
break;
}
case 1:
{
//Moving Right
if(transform.position.x < maxDist)
{
GetComponent <Rigidbody2D>().velocity = new Vector2(movingSpeed,GetComponent<Rigidbody2D>().velocity.y);
}
else
{
direction = -1;
}
break;
}
}
}
}
Thank you very much!