study/first_study/Assets/Scripts/EnemySc/Mob.cs
jh04010421 076bb9758d 윤지호 | 유니티 연습
20226.01.28 수정 (태그,몬스터 구현완료)
2026-01-28 21:32:29 +09:00

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("»ý°¢!");
nextMove = Random.Range(-1, 2);
float nextThinkTime = Random.Range(2f, 5f);
Invoke("Think", nextThinkTime);
anim.SetInteger("WalkSpeed", nextMove);
}
}