๋ญ๊ฐ€ ๋ฌธ์ œ์ธ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐย public Camera myCamera; ,ย public GameObject player; ์— ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋ญ๊ฐ€ ๋ฌธ์ œ์ผ๊นŒ์š”

public Camera์—๋Š” ๋ฉ”์ธ์นด๋ฉ”๋ผ, public GameObject์—๋Š” ํ”Œ๋ ˆ์ด์–ด๋ฅผ ์‚ฝ์ž…ํ•  ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค.



using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class LaserGenerator : MonoBehaviour

{

ย  ย  public Transform spawnPoint;

ย  ย  public GameObject LaserPrefab;

ย  ย  GameObject myPrefab;

ย  ย  public Camera myCamera;

ย  ย  public Transform cameraTransform;

ย  ย  public GameObject player;


ย  ย  float Generate_Cycle = 0;

ย  ย  float cameraMainX;

ย  ย  float myPrefabX;

ย  ย  float playerX;


ย  ย  //float LaserForce = 1f;

ย  ย  void Start()

ย  ย  {

ย  ย  ย  ย  player = GameObject.Find("player");

ย  ย  ย  ย  myCamera = Camera.main;

ย  ย  ย  ย  cameraTransform = myCamera.transform;

ย  ย  }


ย  ย  // Update is called once per frame

ย  ย  void Update()

ย  ย  {

ย  ย  ย  ย  this.Generate_Cycle += Time.deltaTime;

ย  ย  ย  ย  if(Generate_Cycle > 1f)

ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  myPrefab = Instantiate(LaserPrefab, spawnPoint.position, spawnPoint.rotation);

ย  ย  ย  ย  ย  ย  Generate_Cycle = 0;

ย  ย  ย  ย  }


ย  ย  ย  ย  cameraMainX = myCamera.transform.position.x;

ย  ย  ย  ย  myPrefabX = myPrefab.transform.position.x;

ย  ย  ย  ย  playerX = player.transform.position.x;


ย  ย  ย  ย  if (myPrefab != null)

ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  if (myPrefabX > cameraMainX + 29f)

ย  ย  ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  ย  ย  Destroy(GameObject.Find("Laser_Prefab(Clone)"));

ย  ย  ย  ย  ย  ย  }

ย  ย  ย  ย  ย  ย  if (myPrefabX > playerX + 29f)

ย  ย  ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  ย  ย  Destroy(GameObject.Find("Laser_Prefab(Clone)"));

ย  ย  ย  ย  ย  ย  }

ย  ย  ย  ย  }

ย  ย  }


ย  ย  //void FixedUpdate()

ย  ย  //{

ย  ย  //ย  ย  cameraMainX = cameraMain.transform.position.x;

ย  ย  //ย  ย  myPrefabX = myPrefab.transform.position.x;

ย  ย  //}

}