Unity fishing game development tutorial and source code

Renderings show

Project Analysis

Main function points:

  • fish movement route
    • A simple movement method is used here: random position and then random fish straight line or update the angle of the fish every frame to achieve circular movement.
  • The gun moves with the mouse or click position
    • This uses coordinate conversion reference code
    •  private void Update()
          {
              Vector3 mousePos; // 鼠标位置
              // RectTransformUtility.ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint);
              // 将屏幕空间点转换为位于给定 RectTransform 平面上的世界空间中的位置
              // canvas,鼠标的位置,观察的摄像机,out 参数:把计算得到的值传递回来
              RectTransformUtility.ScreenPointToWorldPointInRectangle(canvas, new Vector2(Input.mousePosition.x, Input.mousePosition.y), mainCamera, out mousePos);
              float z; // z轴的旋转
              if (mousePos.x > transform.position.x) // 鼠标位置在正前方的右边
              {
                  //public static float Angle(Vector3 from, Vector3 to); // 两个向量的角度
                  // Vector3.up:正前方,mousePos - transform.position:鼠标到枪的向量
                  z = -Vector3.Angle(Vector3.up, mousePos - transform.position);
              }
              else // 鼠标位置在正前方的左边
              {
                  z = Vector3.Angle(Vector3.up, mousePos - transform.position);
              }
              // 设置枪的目标位置
              transform.localRotation = Quaternion.Euler(0, 0, z);
          }
  • Bullet generation and destruction
    • After the bullet is generated, set a certain amount of time to automatically destroy it. Of course, you can also create an object pool to improve performance.
  • bullet hits fish
    • Add coliider to fish and perform collision detection (OnTriggerEnter2D)

Project source code

Guess you like

Origin blog.csdn.net/st75033562/article/details/133754962