Преглед изворни кода

Вращение камеры вокруг объекта

Andrey Ushakov пре 1 недеља
родитељ
комит
954896d7e8

+ 6 - 6
SpaceCraft.Unity/Assets/_Project/Scenes/Game.unity

@@ -211,8 +211,8 @@ Transform:
   m_PrefabAsset: {fileID: 0}
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 330585543}
   m_GameObject: {fileID: 330585543}
   serializedVersion: 2
   serializedVersion: 2
-  m_LocalRotation: {x: 0.2293313, y: 0.2598835, z: -0.06366815, w: 0.93584937}
-  m_LocalPosition: {x: -20.203243, y: 21.4536, z: -44.611008}
+  m_LocalRotation: {x: -0.2830431, y: -0.30966485, z: 0.09539603, w: -0.9027148}
+  m_LocalPosition: {x: -20, y: 20, z: -20}
   m_LocalScale: {x: 1.9811002, y: 1.9810998, z: 1.9811}
   m_LocalScale: {x: 1.9811002, y: 1.9810998, z: 1.9811}
   m_ConstrainProportionsScale: 0
   m_ConstrainProportionsScale: 0
   m_Children: []
   m_Children: []
@@ -274,8 +274,8 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 6f07d2758859e484caa3b2d871ba819c, type: 3}
   m_Script: {fileID: 11500000, guid: 6f07d2758859e484caa3b2d871ba819c, type: 3}
   m_Name: 
   m_Name: 
   m_EditorClassIdentifier: Assembly-CSharp::CameraController
   m_EditorClassIdentifier: Assembly-CSharp::CameraController
-  player: {fileID: 8680486949395002411}
-  offset: {x: -7, y: 3, z: -7}
+  player: {fileID: 1579309476}
+  offset: {x: -20, y: 20, z: -20}
 --- !u!1 &410087039
 --- !u!1 &410087039
 GameObject:
 GameObject:
   m_ObjectHideFlags: 0
   m_ObjectHideFlags: 0
@@ -920,7 +920,7 @@ GameObject:
   - component: {fileID: 1579309477}
   - component: {fileID: 1579309477}
   - component: {fileID: 1579309481}
   - component: {fileID: 1579309481}
   m_Layer: 0
   m_Layer: 0
-  m_Name: Plane
+  m_Name: PlaneGenerator
   m_TagString: Untagged
   m_TagString: Untagged
   m_Icon: {fileID: 0}
   m_Icon: {fileID: 0}
   m_NavMeshLayer: 0
   m_NavMeshLayer: 0
@@ -1005,7 +1005,7 @@ Transform:
   m_GameObject: {fileID: 1579309476}
   m_GameObject: {fileID: 1579309476}
   serializedVersion: 2
   serializedVersion: 2
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: -10.01, y: 0.09, z: -19.95}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_ConstrainProportionsScale: 0
   m_ConstrainProportionsScale: 0
   m_Children: []
   m_Children: []

+ 36 - 4
SpaceCraft.Unity/Assets/_Project/Scripts/Game/CameraController.cs

@@ -1,19 +1,51 @@
 using UnityEngine;
 using UnityEngine;
+using UnityEngine.InputSystem;
 
 
 public class CameraController : MonoBehaviour
 public class CameraController : MonoBehaviour
 {
 {
     public GameObject player;
     public GameObject player;
     public Vector3 offset;
     public Vector3 offset;
+    public float distance = 20;
+
+    private Vector2 mouseOrigin;
+    private Vector3 cameraOrigin;
+
+    private bool drag = false;
 
 
     // Start is called once before the first execution of Update after the MonoBehaviour is created
     // Start is called once before the first execution of Update after the MonoBehaviour is created
-    void Start()
+    private void Start()
     {
     {
-        
+
     }
     }
 
 
     // Update is called once per frame
     // Update is called once per frame
-    void LateUpdate()
+    private void LateUpdate()
     {
     {
-        transform.position = player.transform.position + offset;
+        if (Mouse.current.rightButton.isPressed)
+        {
+            var mousePosition = Mouse.current.position.value;
+
+            if (drag == false)
+            {
+                drag = true;
+                mouseOrigin = mousePosition;
+                cameraOrigin = Camera.main.transform.position;
+            }
+
+        }
+        else
+        {
+            drag = false;
+        }
+
+        if (drag)
+        {
+            var mousePosition = Mouse.current.position.value;
+            var mouseDiff = mouseOrigin - mousePosition;
+            var mouseDiff3 = (Vector3)mouseDiff;
+            mouseOrigin = mousePosition;
+            Camera.main.transform.Translate(mouseDiff3);
+            Camera.main.transform.rotation = Quaternion.LookRotation(player.transform.position - Camera.main.transform.position);
+        }
     }
     }
 }
 }

+ 46 - 27
SpaceCraft.Unity/Assets/_Project/Scripts/Game/PlaneGenerator.cs

@@ -14,6 +14,7 @@ public class PlaneGenerator : MonoBehaviour
     void Start()
     void Start()
     {
     {
         BuildRootQuads();
         BuildRootQuads();
+        UpdateMeshes();
     }
     }
 
 
     // Update is called once per frame
     // Update is called once per frame
@@ -23,36 +24,54 @@ public class PlaneGenerator : MonoBehaviour
     }
     }
 
 
     private void BuildRootQuads()
     private void BuildRootQuads()
+    {
+        quads = new List<Quad>();
+
+        var quad = BuildQuad("quadY");
+        quads.Add(quad);
+        RotatePlaneY(quad.Vertices);
+
+        quad = BuildQuad("quadY1");
+        quads.Add(quad);
+        RotatePlaneY1(quad.Vertices);
+
+        quad = BuildQuad("quadZ");
+        quads.Add(quad);
+        RotatePlaneZ(quad.Vertices);
+
+        quad = BuildQuad("quadZ1");
+        quads.Add(quad);
+        RotatePlaneZ1(quad.Vertices);
+
+        quad = BuildQuad("quadX");
+        quads.Add(quad);
+        RotatePlaneX(quad.Vertices);
+
+        quad = BuildQuad("quadX1");
+        quads.Add(quad);
+        RotatePlaneX1(quad.Vertices);
+    }
+
+    private Quad BuildQuad(string name)
     {
     {
         var (vertices, uv) = CreateVertices();
         var (vertices, uv) = CreateVertices();
         var triangles = CreateTriangles();
         var triangles = CreateTriangles();
-        RotatePlaneY(vertices);
-        CreateMesh("mesh_Y", vertices, uv, triangles);
-
-        (vertices, uv) = CreateVertices();
-        triangles = CreateTriangles();
-        RotatePlaneY1(vertices);
-        CreateMesh("mesh_Y1", vertices, uv, triangles);
-
-        (vertices, uv) = CreateVertices();
-        triangles = CreateTriangles();
-        RotatePlaneZ(vertices);
-        CreateMesh("mesh_Z", vertices, uv, triangles);
-
-        (vertices, uv) = CreateVertices();
-        triangles = CreateTriangles();
-        RotatePlaneZ1(vertices);
-        CreateMesh("mesh_Z1", vertices, uv, triangles);
-
-        (vertices, uv) = CreateVertices();
-        triangles = CreateTriangles();
-        RotatePlaneX(vertices);
-        CreateMesh("mesh_X", vertices, uv, triangles);
-
-        (vertices, uv) = CreateVertices();
-        triangles = CreateTriangles();
-        RotatePlaneX1(vertices);
-        CreateMesh("mesh_X1", vertices, uv, triangles);
+
+        return new Quad
+        {
+            Name = name,
+            Vertices = vertices,
+            Triangles = triangles,
+            Uv = uv
+        };
+    }
+
+    private void UpdateMeshes()
+    {
+        foreach (var quad in quads)
+        {
+            CreateMesh(quad.Name, quad.Vertices, quad.Uv, quad.Triangles);
+        }
     }
     }
 
 
     private (Vector3[] vertices, Vector2[] uv) CreateVertices()
     private (Vector3[] vertices, Vector2[] uv) CreateVertices()

+ 15 - 5
SpaceCraft.Unity/Assets/_Project/Scripts/Game/Quad.cs

@@ -2,13 +2,23 @@ using UnityEngine;
 
 
 public struct Quad
 public struct Quad
 {
 {
-    public int id { get; set; }
+    public int Id { get; set; }
 
 
-    public int parentId { get; set; }
+    public string Name { get; set; }
 
 
-    public int level { get; set; }
+    public int ParentId { get; set; }
 
 
-    public Vector3 position { get; set; }
+    public int Level { get; set; }
 
 
-    public Quaternion rotation { get; set; }
+    public Vector3[] Vertices { get; set; }
+
+    public int[] Triangles { get; set; }
+
+    public Vector2[] Uv { get; set; }
+
+    public Mesh Mesh { get; set; }
+
+    public Vector3 Position { get; set; }
+
+    public Quaternion Rotation { get; set; }
 }
 }