(4B)raycast


何かにぶつかったらゆっくり消えるスクリプト作らなければってなった時のraycast

public class Contact : MonoBehaviour
{
    //RaycastHitの「入れ物」
    RaycastHit hit;
    Vector3 iremono;
    //public float speed = 350.0f;
    int counta;

    void Start()
    {
    }
    void Update()
    {

        //最大のポイント
        //クリックしたら、クリック位置にRayを飛ばして色々物色
        if (Input.GetMouseButtonDown(0))
        {
            //elseの何もぶつかっていない時のマウス位置を取得用
            Vector3 pos = Input.mousePosition;

            //posにやや奥行を設定
            pos.z = 10.0f;

            //クリック位置にRayを発射!!
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            //rayの可視化(Sceneで見ないと出ない)
            float distance = 100; // 飛ばす&表示するRayの長さ
            float duration = 3;   // 表示期間(秒)
            Debug.DrawRay(ray.origin, ray.direction * distance, Color.red, duration, false);

            //Rayが当たったオブジェクトの情報を入れる箱
            RaycastHit hit = new RaycastHit();

            //rayにぶつかったら
            if (Physics.Raycast(ray, out hit))
            {
                counta++;
                // 複数クリック対策
                if (counta == 1)
                {
                    iTween.FadeTo(gameObject, iTween.Hash(
                    "alpha", 0,
                    "time", 2f,
                    "oncomplete", "Destroy",
                    "oncompletetarget", this.gameObject
                     ));

                    // Rayの衝突地点に、このスクリプトがアタッチされているオブジェクトを移動させる
                    this.transform.position = hit.point;

                    // コルーチンで一時停止
                    // StartCoroutine("ChangeTime");
                }
            }
            //rayに何もぶつかっていない場合はキューブを移動
            else
            {
                Vector3 newpos = Camera.main.ScreenToWorldPoint(pos);
                transform.position = newpos;
            }
        }
    }


private void Destroy()
{
    Destroy(gameObject);
}

    IEnumerator ChangeTime()
    {
        //赤色にする
        //gameObject.GetComponent<Renderer>().material.color = Color.red;

        //2秒停止
        yield return new WaitForSeconds(2);

        //自分を消す
        Destroy(this.gameObject);
    }
}

解決事項

iTween.Fade問題

pointと不明点

・Raycastの使用(検証)
 これの違い
 ・ScreenPointToRay
 ・ScreenToWorldPoint

・iTweenアセットの利用(検証)
 もしや、ゆっくり移動可能?出来る事をもっと知りたい

・移動させる部分(復元検証)
 // Rayの衝突地点に、このスクリプトがアタッチされているオブジェクトを移動させる
this.transform.position = hit.point; 

 この部分がtransrateだとうまく動かなかった。
 当たった位置をVector3で格納してその位置にtransrateしたと思う。。

・初めてのコルーチン処理
 IEnumerator

・OnTriger(復元検証)
 その前段で、衝突をOnTrigerで取得してVector3に格納して使おうと思ったけどだめだった。
 Vector3で保管、もしくは値のセーブ方法