我创建了我的第一个统一的游戏,我有一个敌人角色,从左上角移动到右侧完全正常,但后来我有一个预制(该字符的复印件)那突然停止移动,但是当我拿起他与位置工具角色开始再次移动,然后再次停止。我正在使用Unity 5.2.3f1版本。游戏人物停止移动
下面是我使用
public class EnemyPatrol : MonoBehaviour {
public float moveSpeed;
public bool moveRight;
public float wallCheckRadius;
public Transform wallCheck;
public LayerMask whatIsWall;
private bool hittingWall;
private bool notAtEdge;
public Transform edgeCheck;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
//GetComponent<Rigidbody2D>().velocity=new Vector2(3,0);
//Debug.Log("Premikam se desno "+GetComponent<Rigidbody2D>().velocity);
//hittingWall = Physics2D.OverlapCircle (wallCheck.position, wallCheckRadius, whatIsWall);
wallCheckRadius = 0.6f;
notAtEdge = Physics2D.OverlapCircle (edgeCheck.position, wallCheckRadius, whatIsWall);
hittingWall = false;
//notAtEdge = true;
if (hittingWall || !notAtEdge) {
moveRight = !moveRight;
Debug.Log ("Zadene steno " + hittingWall + " Ni na robu " + notAtEdge);
}
// Prepreci krozenje igralca (zabavna zadeva)
GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
if (moveRight) {
transform.localScale = new Vector3 (1f, 1f, 1f);
GetComponent<Rigidbody2D>().velocity=new Vector2(3,0);
Debug.Log("Premikam se desno "+GetComponent<Rigidbody2D>().velocity);
} else {
transform.localScale = new Vector3 (-1f, 1f, 1f);
GetComponent<Rigidbody2D>().velocity=new Vector2(-3,0);
Debug.Log("Premikam se levo"+GetComponent<Rigidbody2D>().velocity);
}
}
}
的代码,这是它的外观在游戏中的画面。
- 圈撞机显示了“杀戮地带”的撞机范围内,如果玩家接触它他死了。
- 箱撞机检查,如果他是在地面上
- 绿球检查,如果他得到了拐角
- 黄球检查,如果他撞墙(我在这里并没有真正使用,只是为了以防万一我需要它)