AI Enemy Unity-Monsters patrol and cross rivers
AI Enemy Unity-Monsters patrol and cross rivers
C#:
1:enemy_controller
[SerializeField] Transform left;
[SerializeField] Transform right;
[SerializeField] Transform enemy_Tranform;
[SerializeField] float speed;
[SerializeField] Animator anim;
[SerializeField] float idleDuration;
float idleTime;
Vector3 initScale;
bool movingLeft;
void Awake()
{
this.initScale = this.enemy_Tranform.localScale;
}
private void OnDisable() {
anim.SetBool("run",false);
}
void Update()
{
if(this.movingLeft)
{
if(this.enemy_Tranform.position.x >= this.left.position.x)
MoveInDirection(-1);
else
DirectionChange();
}
else
{
if(this.enemy_Tranform.position.x <= this.right.position.x)
MoveInDirection(1);
else
DirectionChange();
}
}
private void DirectionChange()
{
anim.SetBool("run",false);
idleTime += Time.deltaTime;
if(idleTime > idleDuration)
movingLeft = !movingLeft;
}
void MoveInDirection(int _direction)
{
idleTime = 0;
anim.SetBool("run",true);
//face
this.enemy_Tranform.localScale = new Vector3(this.initScale.x * _direction,
this.initScale.y,this.initScale.z);
//move
this.enemy_Tranform.position = new Vector3(this.enemy_Tranform.position.x +
Time.deltaTime *_direction * this.speed,this.enemy_Tranform.position.y,
this.enemy_Tranform.position.z);
}
2:enemy_demo
[SerializeField] enemy_controller enemy_C;
[SerializeField] Transform location1;
[SerializeField] Transform location2;
[SerializeField] Animator anim;
[SerializeField] float speed;
[SerializeField]bool facingRight=false;
float value =3f;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
//boat docked
if(boat.instance.ideLeft==true)
{
if( Vector2.Distance(this.transform.position,this.location1.position)>0.1f
&& boat.instance.number == 1)
{
Vector3 thescale = this.transform.localScale;
if(thescale.x ==1)
FlipEnemy(location1);
this.enemy_C.enabled=false;
anim.SetBool("run",true);
this.transform.position = Vector3.MoveTowards(this.transform.position,
this.location1.position,speed*Time.deltaTime);
}
if(Vector2.Distance(this.transform.position,this.location2.position)>0.1f
&& boat.instance.number == 2)
{
Vector3 thescale = this.transform.localScale;
if(thescale.x == -1)
{
this.facingRight=false;
FlipEnemy(location2);
}
anim.SetBool("run",true);
this.transform.position = Vector3.MoveTowards(this.transform.position,
this.location2.position,speed*Time.deltaTime);
}
}
//the boat leaves the wharf
else
{
//standing on the boat
if(boat.instance.number == 1)
{
anim.SetBool("run",false);
this.transform.position = this.location1.position;
}
//on patrol
else
{
this.enemy_C.enabled=true;
}
}
}
void FlipEnemy(Transform face)
{
if(this.transform.position.x < face.position.x && facingRight)
{
flip();
}
else if(this.transform.position.x > face.position.x && !facingRight)
{
flip();
}
}
void flip()
{
Vector3 thescale = this.transform.localScale;
thescale.x *= -1;
this.transform.localScale = thescale;
this.facingRight = !this.facingRight;
}
3:boat
public static boat instance;
[SerializeField] Transform left;
[SerializeField] Transform right;
[SerializeField] float speed;
[SerializeField] float idleDuration;
float idleTime;
bool movingLeft;
public bool ideLeft;
public int number;
public bool numberadd;
void Awake()
{
instance = this;
this.number=0;
}
void Update()
{
if(this.movingLeft)
{
if(this.transform.position.x >= this.left.position.x)
MoveInDirection(-1);
else
DirectionChange();
}
else
{
if(this.transform.position.x <= this.right.position.x)
MoveInDirection(1);
else
DirectionChange();
}
if( Vector2.Distance(this.transform.position,this.left.position)<0.2f)
{
this.ideLeft=true;
if(this.numberadd ==false)
{
if(this.number ==2)
{
this.number =0;
}
else
{
number += 1;
}
this.numberadd=true;
}
}
else
{
this.ideLeft=false;
this.numberadd=false;
}
}
private void DirectionChange()
{
idleTime += Time.deltaTime;
if(idleTime > idleDuration)
movingLeft = !movingLeft;
}
void MoveInDirection(int _direction)
{
idleTime = 0;
//move
this.transform.position = new Vector3(this.transform.position.x +
Time.deltaTime *_direction * this.speed,this.transform.position.y,
this.transform.position.z);
}
Post a Comment