unity2D 自動で動く敵の作成

まずは,敵キャラ,playerの画像をダウンロード,importしていきます.

 

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using UnityEditor;

public class enemy : MonoBehaviour {
    Rigidbody2D rb;
    private int moveSpeed = 2;

    private int direction=-1;

    void Start (){
        //GetComponentの処理をキャッシュしておく
        rb = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate () {
        //velocity: 速度
        //X方向へmoveSpeed分移動させる
        rb.velocity = new Vector2(moveSpeed*direction, rb.velocity.y);
    }

    void OnCollisonEnter2D(Collision2D other){
        if(other.gameObject.CompareTag("wall")){
            direction = direction * -1;
            Debug.Log("壁に当たった");
        }
    }
}

 上が敵のScriptです.しかし,壁に当たっても反転してくれません!!

これから壁の判定を考えていきましょ〜〜

 

こちらは,参考にさせていただいたサイトです.

hiyotama.hatenablog.com