Middle Monster의 총알이 player를 향하도록 설정
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class MonsterBulletController : MonoBehaviour
{
private Rigidbody2D bulletRigidbody;
public float speed = 0.7f;
public float lifeTime = 3f;
// Start is called before the first frame update
void Start()
{
bulletRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
// 총알을 아래로 발사
bulletRigidbody.AddForce(Vector2.down * speed, ForceMode2D.Impulse);
Destroy(bulletRigidbody, lifeTime);
}
}
GetComponent < ComponentType > ();
: 지정된 GameObject 유형의 구성 요소에 대한 참조를 가지고 온다.
: 매개변수 = Component의 구성 요소의 type
: 기본 반환값 = null
ex) myResults = otherGameObject.GetComponent< ComponentType >();
GameObject를 연결하여 유니티 내부 스크립트 class에서 코드를 작성할 경우
: GameObject는 해당 연결된 GameObject의 구성 요소를 가져올 수 있습니다. 이 경우, GameObject를 생략하여도 같은 결과를 가져옵니다.
ex) myResults = GetComponent< ComponentType >()
ex) public GameObject name;
Rigidbody에 힘을 추가하여 vector 방향을 따라 연속적으로 적용되며, 힘의 가속도, 충격량, 속도를 변경할 수 있습니다.
AddForce(Vector3 force, ForceMode mode = ForceMode.Force);
AddForce (float x , float y , float z , ForceMode 모드 = ForceMode.Force);
: force = global 좌표의 힘 (x, y, z)
: ForceMode = 적용할 힘의 유형
GameObject 구성 요소 또는 자산을 제거합니다.
Object가 Component이 경우 이 메서드는 GameObject에서 구성 요소를 제거하고 파괴합니다. GameObject인 경우 GameObject, 모든 구성 요소들, GameObject의 모든 변형 자식 또한 삭제됩니다.
Destroy(Object obj, float t = 0.0F);
: Obejct = 삭제할 객체
: time = 객체를 삭제하기 전에 지연할 수 있도록 설정(선택)
추가 ex) Destroy(GetComponent< BoxCollider >());
Destroy