クリック(タッチ)したら何か起こるけど、プレイヤーとは当たり判定が無いオブジェクトを作る

【目的】

2Dの横スクロールアクションを作っています。

クリック(スマホではタップ、以下クリック)できるオブジェクト(ボールとします)があって、

クリックするとプレイヤーがそのボールに向かってジャンプする(加速する)ものとします。

目的は、ボールとプレイヤーの当たり判定をなくすことです。


【プレイヤーの設定】

・Spriteとしてオブジェクトを作ります。

・Rigidbody 2D、Box Collider 2Dをアタッチ

・Layerをplayerなど、プレイヤーにのみ適用されるレイヤーを作って設定する

・ジャンプするスクリプトをアタッチ

例:

public class ActorController : MonoBehaviour
{
    Transform myTransform;
    private Rigidbody2D rigidbody2D;
    float v_factor=10f;//6f;

    // Start is called before the first frame update
    void Start()
    {
        // コンポーネント参照取得
        rigidbody2D = GetComponent<Rigidbody2D> ();
        // transformを取得
        myTransform = this.transform;
        Physics.gravity = new Vector3(0,20,0);
    }

    float Abs(Vector2 v){
        return Mathf.Sqrt(v.x*v.x+v.y*v.y);
    }

    public void Jamp(){
//            Vector2 mousePosition = Input.mousePosition;
            Debug.Log("Jamp");
            Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Debug.Log("LeftClick:"+mousePosition );
            Vector2 ThisPosition = myTransform.position;
            Debug.Log("LeftClick, actor's position=:"+ThisPosition );
            Vector2 direction_to_tatch=mousePosition-ThisPosition;
            Debug.Log("direction_to_tatch=:"+direction_to_tatch);
            float normalization=Abs(direction_to_tatch);
            Debug.Log("normalization=:"+normalization);
            Vector2 v_actor=direction_to_tatch/normalization*v_factor;
//            Vector2 v_actor=direction_to_tatch*v_factor;
            rigidbody2D.velocity = new Vector2 (v_actor.x, v_actor.y);
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.rotation = Quaternion.Euler(0, 0, 0);///回転を止める
    }
}


【ボールの設定】

・Spriteとしてオブジェクトを作る。(名前はJampSphere)

・Box Collider 2Dをアタッチ


【クリックしたときの反応を処理するオブジェクトを用意】

・SceneManagerなる空のgame objectを作成

・タッチ処理するスクリプトをアタッチ

例:

public class SceneManager : MonoBehaviour
{
    public string Jamp1Tag="Jamp1";
    public ActorController ActorController;
    GameObject clickedGameObject;

    // Start is called before the first frame update

    // Update is called once per frame  
    void Update()
    {
       
        if (Input.GetMouseButtonDown(0))
        {
            clickedGameObject=null;
            Ray ray = new Ray();
            RaycastHit hit = new RaycastHit();
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit2d = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction);
            Debug.Log("click");

            if (hit2d)
            {
                clickedGameObject = hit2d.transform.gameObject;
                Debug.Log("clickedGameObject.name="+clickedGameObject.name);
                if(clickedGameObject.name=="JampSphere"){
                    Debug.Log(ActorController.name);//ゲームオブジェクトの名前を出力
                    ActorController.Jamp();
                }
            }
        }
    }
}


これで以下のような挙動になります。


ちゃんとクリックしたところに向かってジャンプして、

なおかつJampSphereともぶつかりません。

(このままだとめっちゃ難しいゲームですね)




コメント

このブログの人気の投稿

【TopDown Engine】見下ろし2Dアクションゲームのためのアセット

初投稿

【コルーチン】処理順を大事にしたい時、処理を待ちたい時に使うコルーチンについて