/// <summary> /// 要發射飛行武器的時候,呼叫這個方法。 /// </summary> public void Shot(Vector2 direction) { BulletUnit bu = Instantiate(_bulletClone); bu.transform.position = _NinjaVisual.position; // 如果玩家沒有按任何方向鍵,預設往忍者前方射 if (direction.x == 0 && direction.y == 0) { bu.GetComponent <Rigidbody2D>().AddForce(Vector2.right * _NinjaVisual.localScale.x * _flySpeed); } else if (direction.x >= 0 && Mathf.Abs(direction.x) >= Mathf.Abs(direction.y)) { bu.GetComponent <Rigidbody2D>().AddForce(Vector2.right * _flySpeed); } else if (direction.x < 0 && Mathf.Abs(direction.x) >= Mathf.Abs(direction.y)) { bu.GetComponent <Rigidbody2D>().AddForce(Vector2.left * _flySpeed); } else if (direction.y >= 0 && Mathf.Abs(direction.y) > Mathf.Abs(direction.x)) { bu.GetComponent <Rigidbody2D>().AddForce(Vector2.up * _flySpeed); } else if (direction.y < 0 && Mathf.Abs(direction.y) > Mathf.Abs(direction.x)) { bu.GetComponent <Rigidbody2D>().AddForce(Vector2.down * _flySpeed); } }
public void CreateBullet(string resPath, Vector3 startPos, Vector3 touchPos) { var uuid = System.Guid.NewGuid().ToString("N"); var obj = ResourceMgr.instance.Instantiate <GameObject>(resPath); obj.transform.SetParent(m_rootTrans, false); obj.transform.position = startPos; var direction = (touchPos - startPos).normalized; obj.transform.forward = direction; BulletUnit unit = new BulletUnit(uuid, obj, direction, 30); //加入到容器中 m_bulletDic.Add(unit.uuid, unit); }