- Home /
How can I make my creature change a direction?
I have a creature that is moving randomly, but when it hits a "wall" it changes a direction, but I want to go on some random direction, not to move like a ping-pong ball. Can anyone help me with this? Here's my code.
using UnityEngine;
using System.Collections;
public class Random_moving : MonoBehaviour{
public Vector3 target;
public float speed=10f;
int direction = 1;
void Start (){
target = Random.insideUnitSphere * 5;
transform.Rotate(target);
}
void FixedUpdate() {
rigidbody2D.MovePosition(rigidbody2D.position + ((Vector2)(transform.forward * speed * Time.deltaTime * direction)));
}
void OnCollisionEnter2D (Collision2D coll){
Debug.Log ("hit.");
if (coll.gameObject.tag == "maze" || coll.gameObject.tag == "coin"){
if (screenPos.x < Screen.width / 2){
direction *= -1;
Debug.Log ("change_2.");
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Why Scene view don't show anything but game view is ok with NGUI ? 1 Answer
Multiple Cars not working 1 Answer
How can I make the score counter use the custom font? 1 Answer
A node in a childnode? 1 Answer