//碰撞 void Collider_enter(GameObject _collider) { My2Dphysic rigid = _collider.GetComponent <My2Dphysic>(); //被撞到時: if (rigid != null && rigid.isEntity) { //能量轉移 Vector2 _trans_force = (velocity) * mass; //Debug.Log(gameObject.name + " V: " + (rigid.velocity) + " enter " + _collider.name + " V: " + (velocity) + " ENTER FORCE:" + _trans_force); rigid.AddForce(_trans_force); } }
void Collider_stay(GameObject _collider) { My2Dphysic rigid = _collider.GetComponent <My2Dphysic>(); //把對方推開: if (rigid != null && rigid.isEntity) { Vector2 dir = transform.position - rigid.transform.position; //Vector2 dir = rigid.move_dir; //[bug]會滑掉 rigid.AddForce(dir * mass * Time.deltaTime); } }
//移動 IEnumerator Move_coro() { //到達目標? while (Vector2.Distance(transform.position, moveGoal.transform.position) > reach_radious) { //是否碰到牆? if (rigid.is_near_wall) { //JUMP //if (jumpable) if (jump_force > 0) { rigid.AddForce(Vector2.up * jump_force); if (jump_event != null) { jump_event(); Debug.Log("<color=green>JUMP</color>"); } yield return(new WaitForFixedUpdate()); } } //平地移動 Vector2 _dir = moveGoal.transform.position - transform.position; rigid.velocity.x = _dir.normalized.x * speed; if (walk_event != null) { walk_event(); Debug.Log("<color=green>WALK</color>"); } yield return(new WaitForFixedUpdate()); } //到達:移動結束 rigid.velocity.x = 0; reached_goal = true; if (idle_event != null) { idle_event(); Debug.Log("<color=green>IDLE</color>"); } //等待後重新找目標 yield return(new WaitForSeconds(move_gap_time)); if (!is_chasing_target) { Set_move_goal(); } _move_coro = null; }