- Home /
How to create 2D random movement?
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. i don't know why? In my scene I have maze with attached box colliders 2d with unchecked isTrigger and use by effector.
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);
}
}
Answer by Duugu · Aug 28, 2015 at 09:40 PM
You're probably using a wrong combination of static colliders/rigidbodies/events. Please check the table at the bottom of this article to see if your setup is valid. If your setup is valid you could check the collision matrix in the physics manager.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
How to stop MoveTowards teleporting 1 Answer
2d obstacle moving up and down question 1 Answer
Distribute terrain in zones 3 Answers