So far I have code which translates my game object in random position, but it does not detect when it’s collided with box collider 2D. It just shivers in random direction, I don’t know why? In my scene I have maze with attached box colliders 2d with unchecked isTrigger and use by effector (screenshots).
using UnityEngine;
using System.Collections;
public class randomMovement : MonoBehaviour {
public int ranNum;
public GameObject blinky;
public float speed = 0.3f;
// Use this for initialization
void Start () {
ranNum = 1;
}
// Update is called once per frame
void FixedUpdate () {
ranNum = Random.Range (1, 5);
switch (ranNum) {
case 1:
blinky.transform.Translate(Vector2.up*speed);
break;
case 2:
blinky.transform.Translate(-Vector2.up*speed);
break;
case 3:
blinky.transform.Translate(Vector2.right*speed);
break;
case 4:
blinky.transform.Translate(-Vector2.right*speed);
break;
}
}
void OnCollisionEnter2D(Collision2D col){
Debug.Log ("Collided!");
if (col.gameObject.name == "maze")
ranNum = Random.Range (1, 5);
}
}