58 lines
1.3 KiB
C#
58 lines
1.3 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
public class Mob : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
//[SerializedField] private MobBase mobBase;
|
|||
|
|
|
|||
|
|
Rigidbody2D rbody;
|
|||
|
|
Animator anim;
|
|||
|
|
|
|||
|
|
public int nextMove;
|
|||
|
|
public float axisH = 0.0f;
|
|||
|
|
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
rbody = GetComponent<Rigidbody2D>();
|
|||
|
|
anim = GetComponent<Animator>();
|
|||
|
|
Invoke("Think",1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
axisH = rbody.linearVelocity.x;
|
|||
|
|
|
|||
|
|
float direction = axisH > 0 ? 1 : -1;
|
|||
|
|
transform.localScale = new Vector2(direction, 1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void FixedUpdate()
|
|||
|
|
{
|
|||
|
|
rbody.linearVelocity = new Vector2(nextMove, rbody.linearVelocity.y);
|
|||
|
|
|
|||
|
|
Vector2 frontVec = new Vector2(rbody.position.x + 0.2f*nextMove, rbody.position.y);
|
|||
|
|
Debug.DrawRay(frontVec, Vector3.down, new Color(0,1,0));
|
|||
|
|
RaycastHit2D rayHit = Physics2D.Raycast(frontVec, Vector3.down, 1, LayerMask.GetMask("Ground"));
|
|||
|
|
|
|||
|
|
if (rayHit.collider == null)
|
|||
|
|
{
|
|||
|
|
nextMove *= -1;
|
|||
|
|
|
|||
|
|
CancelInvoke();
|
|||
|
|
|
|||
|
|
Invoke("Think", 5);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Think()
|
|||
|
|
{
|
|||
|
|
Debug.Log("<22><><EFBFBD><EFBFBD>!");
|
|||
|
|
nextMove = Random.Range(-1, 2);
|
|||
|
|
|
|||
|
|
float nextThinkTime = Random.Range(2f, 5f);
|
|||
|
|
|
|||
|
|
Invoke("Think", nextThinkTime);
|
|||
|
|
|
|||
|
|
anim.SetInteger("WalkSpeed", nextMove);
|
|||
|
|
}
|
|||
|
|
}
|