【发布时间】:2014-07-10 06:21:03
【问题描述】:
我有一个随机移动的生物,当撞到墙壁时它会改变方向,但一段时间后它会消失(它不应该这样)。谁能告诉我为什么以及如何解决这个问题?这是我的代码:
using UnityEngine;
using System.Collections;
public class Random_moving : MonoBehaviour{
public Vector3 target;
public float speed=10f;
Vector3 screenPos;
int direction = 1;
void Start (){
target = Random.insideUnitSphere * 5;
transform.Rotate(target);
}
void Update (){
transform.position += (transform.forward * speed * Time.deltaTime)*direction;
}
void OnCollisionEnter2D (Collision2D coll){
Debug.Log ("hit.");
if (coll.gameObject.tag == "maze"){
if (screenPos.x < Screen.width / 2){
direction = -1;
Debug.Log ("change_2.");
}
}
}
}
【问题讨论】:
-
请将您的代码复制并粘贴到此问题中。在不访问其他站点的情况下使其完整(可能会损坏)