Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question 以下のスクリプトについてのバグの修正方法を伺いたいです。

Discussion in 'Unity Hub' started by Sou_un_tei, Mar 7, 2023.

  1. Sou_un_tei

    Sou_un_tei

    Joined:
    Mar 7, 2023
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class ShotScript : MonoBehaviour
    {
     private float speed = 100f;
     private GameObject Gem;
     public GameObject Spark1;
     public GameObject Bomb1;
     private Vector3 mousePosition;
     private Vector3 moveDirection;
     private bool isMoving;
     private GameObject instanceShot;
     // Start is called before the first frame update
     void Start()
     {
      Gem = GameObject.FindGameObjectWithTag("Gem");
      isMoving = false;
     }
     // Update is called once per frame
     void Update()
     {
      if(!isMoving &&Input.GetMouseButtonDown(0)){
      isMoving = true;
      mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
      mousePosition.z = 0;
      Vector3 spawnPoint = Gem.transform.position;
      Vector3 direction = mousePosition - spawnPoint;
      float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
      instanceShot = Instantiate(Spark1, spawnPoint, Quaternion.AngleAxis(angle, Vector3.forward)) as   GameObject;
      moveDirection = (mousePosition - instanceShot.transform.position).normalized;
     }
      else if(isMoving){
       if(Input.GetMouseButtonDown(0)){
        return;
       }
      }
      instanceShot.transform.position += moveDirection * speed * Time.deltaTime;
      if(Vector3.Distance(instanceShot.transform.position, mousePosition) < 3f){
      GameObject instanceBomb = Instantiate(Bomb1, instanceShot.transform.position, Quaternion.identity);
      Destroy(instanceShot);
      instanceShot = null;
      isMoving = false;
      }
     }
    }

    以上のスクリプトを起動した際に最初の一回目のマウスクリックで生成されたSpark1プレハブが正常に破棄されず、同時にその際のBomb1プレハブも生成されません。
    また、isMovingがtrueの状態でマウスクリックした際にSpark1プレハブが追加で生成されてしまう不具合が発生しています。
    修正方法を教えていただきたいです。