gif画像のように葉っぱのようなものをUIの葉っぱへ飛ばし、当たれば消滅。といった処理を考えていたのですが、飛んでいくと消えるオブジェクト、消えないオブジェクトの2つが存在しています。
消えないオブジェクトは弾かれたかのような挙動をとります。
解決策として、飛ぶ速度が早すぎるからでは?と思い、数値を落としてみたものの、成果が得られませんでした。
長文になりますが↓がソースコードになっております。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Leaf : MonoBehaviour { 6 7 public float leafAccelspeed;//Leaf加速度設定数値 8 public bool leaf_PlayerFollow;//Leafオブジェクトの追尾機能 true false 9 public float leafSpeed;//Leaf移動速度 10 11 // Use this for initialization 12 void Start () { 13 leaf_PlayerFollow = false; 14 15 16 } 17 18 // Update is called once per frame 19 void Update () { 20 21 //Leaf UIオブジェクトの座標取得 22 var leaf_pos = LeafUI.leaf_instance.transform.position; 23 //Leafオブジェクトと Leaf UIの距離を計算する 24 var distance = Vector3.Distance(leaf_pos, transform.localPosition); 25 26 if (leaf_PlayerFollow) 27 { 28 //Leaf UIのいる位置へ導くベクトルの作成 29 var direction = leaf_pos - transform.localPosition; 30 direction.Normalize(); 31 32 transform.localPosition += direction * leafSpeed; 33 34 leafSpeed += leafAccelspeed; 35 return; 36 } 37 38 } 39 40 41 private void OnTriggerEnter2D(Collider2D collision) 42 { 43 //Playerが当たったら、Leaf UIへのFollwを開始させる 44 if(collision.gameObject.name == "Player") 45 { 46 leaf_PlayerFollow = true; 47 } 48 if (collision.gameObject.tag == "LeafUI") 49 { 50 Destroy(gameObject);//衝突時消滅 51 Debug.Log("消滅!"); 52 } 53 54 } 55 56} 57
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class LeafUI : MonoBehaviour { 7 8 9 public Text LeafCountText;//Leaf取得時増減用 10 public GameObject LeafObj; 11 public float LeafMagnetPower;//マグネットパワー用変数 12 13 public static LeafUI leaf_instance; 14 15 // Use this for initialization 16 void Start () { 17 leaf_instance = this; 18 } 19 20 // Update is called once per frame 21 void Update () { 22 23 } 24 void OnTriggerEnter2D(Collider2D collision) 25 { 26 //Debug.Log("#"); 27 //if (collision.gameObject.tag=="Leaf") 28 //{ 29 // Destroy(gameObject); 30 //} 31 } 32} 33
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/27 00:46
2018/05/27 01:05
2018/05/27 02:07
2018/05/27 03:18