2016-01-23 136 views
0

我创建了我的第一个统一的游戏,我有一个敌人角色,从左上角移动到右侧完全正常,但后来我有一个预制(该字符的复印件)那突然停止移动,但是当我拿起他与位置工具角色开始再次移动,然后再次停止。我正在使用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); 
     } 
    } 
} 

的代码,这是它的外观在游戏中的画面。

enter image description here

- 圈撞机显示了“杀戮地带”的撞机范围内,如果玩家接触它他死了。

- 箱撞机检查,如果他是在地面上

- 绿球检查,如果他得到了拐角

- 黄球检查,如果他撞墙(我在这里并没有真正使用,只是为了以防万一我需要它)

回答

1

你不能在Update中做物理。

......好吧,你可以,但是这打破了Unity。各种奇怪的东西都会发生。在文档中的任何地方,他们都明确表示“不这样做”。

对于涉及物理的任何事情,请使用FixedUpdate。


另外,您应该将脚本附加到探测器本身。您应该将它们作为触发器来实施,并使用OnTriggerEntered等。

让Unity物理部门为您做好工作 - 这就是它的存在。

例如阅读关于:http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html