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

Ajout du script block spawner

Il permet à l'utilisateur de faire spawner un prefab
en face du gameobject auquel le script est attaché

Contrôles :
clic droit (hold) preview du bloc
clic gauche (avec clic droit) placer le bloc
le script est déja attaché au prefab du joueur. Une scene pour le test
est mise en place, avec un petit prefab de bloc a spawner

!! Le bloc de preview a tendance à saccader. Il faudrait y jeter un oeil
!! C'est pas une question de performance ca vient de la manière dont est
!! codé le script. D'ailleurs n'hésitez pas à le réécrire si vous y voyez
!! des lacunes

+ ajout du sprint dans le script free fly
Maj pour sprinter (hold)
shader
Figg пре 4 година
родитељ
комит
38252be267

+ 69
- 0
FreeCam/Assets/BlockSpawner.cs Прегледај датотеку

@@ -0,0 +1,69 @@
1
+using System.Collections;
2
+using System.Collections.Generic;
3
+using UnityEngine;
4
+using UnityEngine.InputSystem;
5
+
6
+public class BlockSpawner : MonoBehaviour
7
+{
8
+    [SerializeField]
9
+    private GameObject _blockToPlace;
10
+    public GameObject BlockToPlace
11
+    {
12
+        get
13
+        {
14
+            return _blockToPlace;
15
+        }
16
+        set
17
+        {
18
+            DisablePreviewMode();
19
+            _blockToPlace = value;
20
+        }
21
+    }
22
+
23
+    public float spawnDistance = 3f;
24
+
25
+    private GameObject _blockInstance;
26
+
27
+    private bool _isPreview = false;
28
+
29
+
30
+    public void Update()
31
+    {
32
+        if (_blockInstance == null) return;
33
+
34
+        _blockInstance.transform.position = transform.position + transform.forward * spawnDistance;
35
+        _blockInstance.transform.rotation = transform.rotation;
36
+    }
37
+
38
+    public void OnPlaceBlock()
39
+    {
40
+        if (!_isPreview) return;
41
+        _blockInstance = null;
42
+        DisablePreviewMode();
43
+    }
44
+
45
+    public void OnTogglePreviewMode(InputValue value)
46
+    {
47
+        float valueAsFloat = value.Get<float>();
48
+
49
+        if (valueAsFloat == 1f) EnablePreviewMode();
50
+        else DisablePreviewMode();
51
+    }
52
+
53
+    private void EnablePreviewMode()
54
+    {
55
+        if (_blockToPlace == null) return;
56
+
57
+        Vector3 pos = transform.position + transform.forward * spawnDistance;
58
+        Quaternion rot = transform.rotation;
59
+        _blockInstance = Instantiate(_blockToPlace, pos, rot);
60
+        _isPreview = true;
61
+    }
62
+
63
+    private void DisablePreviewMode()
64
+    {
65
+        Destroy(_blockInstance);
66
+        Debug.Log("disabled");
67
+        _isPreview = false;
68
+    }
69
+}

+ 11
- 0
FreeCam/Assets/BlockSpawner.cs.meta Прегледај датотеку

@@ -0,0 +1,11 @@
1
+fileFormatVersion: 2
2
+guid: b78dfde35e58d1e4c858653eaf06ea64
3
+MonoImporter:
4
+  externalObjects: {}
5
+  serializedVersion: 2
6
+  defaultReferences: []
7
+  executionOrder: 0
8
+  icon: {instanceID: 0}
9
+  userData: 
10
+  assetBundleName: 
11
+  assetBundleVariant: 

+ 97
- 0
FreeCam/Assets/Block_test.prefab Прегледај датотеку

@@ -0,0 +1,97 @@
1
+%YAML 1.1
2
+%TAG !u! tag:unity3d.com,2011:
3
+--- !u!1 &3300812737117657924
4
+GameObject:
5
+  m_ObjectHideFlags: 0
6
+  m_CorrespondingSourceObject: {fileID: 0}
7
+  m_PrefabInstance: {fileID: 0}
8
+  m_PrefabAsset: {fileID: 0}
9
+  serializedVersion: 6
10
+  m_Component:
11
+  - component: {fileID: 5116820417079337087}
12
+  - component: {fileID: 8129791058919183616}
13
+  - component: {fileID: 6241846484885181946}
14
+  - component: {fileID: 5485846853913691764}
15
+  m_Layer: 0
16
+  m_Name: Block_test
17
+  m_TagString: Untagged
18
+  m_Icon: {fileID: 0}
19
+  m_NavMeshLayer: 0
20
+  m_StaticEditorFlags: 0
21
+  m_IsActive: 1
22
+--- !u!4 &5116820417079337087
23
+Transform:
24
+  m_ObjectHideFlags: 0
25
+  m_CorrespondingSourceObject: {fileID: 0}
26
+  m_PrefabInstance: {fileID: 0}
27
+  m_PrefabAsset: {fileID: 0}
28
+  m_GameObject: {fileID: 3300812737117657924}
29
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
30
+  m_LocalPosition: {x: 2.0756817, y: -0.77946377, z: 9.994711}
31
+  m_LocalScale: {x: 1, y: 1, z: 1}
32
+  m_Children: []
33
+  m_Father: {fileID: 0}
34
+  m_RootOrder: 0
35
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
36
+--- !u!33 &8129791058919183616
37
+MeshFilter:
38
+  m_ObjectHideFlags: 0
39
+  m_CorrespondingSourceObject: {fileID: 0}
40
+  m_PrefabInstance: {fileID: 0}
41
+  m_PrefabAsset: {fileID: 0}
42
+  m_GameObject: {fileID: 3300812737117657924}
43
+  m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
44
+--- !u!23 &6241846484885181946
45
+MeshRenderer:
46
+  m_ObjectHideFlags: 0
47
+  m_CorrespondingSourceObject: {fileID: 0}
48
+  m_PrefabInstance: {fileID: 0}
49
+  m_PrefabAsset: {fileID: 0}
50
+  m_GameObject: {fileID: 3300812737117657924}
51
+  m_Enabled: 1
52
+  m_CastShadows: 1
53
+  m_ReceiveShadows: 1
54
+  m_DynamicOccludee: 1
55
+  m_MotionVectors: 1
56
+  m_LightProbeUsage: 1
57
+  m_ReflectionProbeUsage: 1
58
+  m_RayTracingMode: 2
59
+  m_RayTraceProcedural: 0
60
+  m_RenderingLayerMask: 1
61
+  m_RendererPriority: 0
62
+  m_Materials:
63
+  - {fileID: 2100000, guid: 7a1df68ff2cb2814088ca3c160bfdf17, type: 2}
64
+  m_StaticBatchInfo:
65
+    firstSubMesh: 0
66
+    subMeshCount: 0
67
+  m_StaticBatchRoot: {fileID: 0}
68
+  m_ProbeAnchor: {fileID: 0}
69
+  m_LightProbeVolumeOverride: {fileID: 0}
70
+  m_ScaleInLightmap: 1
71
+  m_ReceiveGI: 1
72
+  m_PreserveUVs: 0
73
+  m_IgnoreNormalsForChartDetection: 0
74
+  m_ImportantGI: 0
75
+  m_StitchLightmapSeams: 1
76
+  m_SelectedEditorRenderState: 3
77
+  m_MinimumChartSize: 4
78
+  m_AutoUVMaxDistance: 0.5
79
+  m_AutoUVMaxAngle: 89
80
+  m_LightmapParameters: {fileID: 0}
81
+  m_SortingLayerID: 0
82
+  m_SortingLayer: 0
83
+  m_SortingOrder: 0
84
+  m_AdditionalVertexStreams: {fileID: 0}
85
+--- !u!65 &5485846853913691764
86
+BoxCollider:
87
+  m_ObjectHideFlags: 0
88
+  m_CorrespondingSourceObject: {fileID: 0}
89
+  m_PrefabInstance: {fileID: 0}
90
+  m_PrefabAsset: {fileID: 0}
91
+  m_GameObject: {fileID: 3300812737117657924}
92
+  m_Material: {fileID: 0}
93
+  m_IsTrigger: 0
94
+  m_Enabled: 1
95
+  serializedVersion: 2
96
+  m_Size: {x: 1, y: 1, z: 1}
97
+  m_Center: {x: 0, y: 0, z: 0}

+ 7
- 0
FreeCam/Assets/Block_test.prefab.meta Прегледај датотеку

@@ -0,0 +1,7 @@
1
+fileFormatVersion: 2
2
+guid: 7b30a4c90d614f747872471cf43b8e56
3
+PrefabImporter:
4
+  externalObjects: {}
5
+  userData: 
6
+  assetBundleName: 
7
+  assetBundleVariant: 

+ 112
- 0
FreeCam/Assets/Controls.inputactions Прегледај датотеку

@@ -20,6 +20,30 @@
20 20
                     "expectedControlType": "Vector2",
21 21
                     "processors": "",
22 22
                     "interactions": ""
23
+                },
24
+                {
25
+                    "name": "Place Block",
26
+                    "type": "Button",
27
+                    "id": "4b7ab0aa-cda3-4160-a713-27c305d29dcd",
28
+                    "expectedControlType": "Button",
29
+                    "processors": "",
30
+                    "interactions": ""
31
+                },
32
+                {
33
+                    "name": "Toggle Preview Mode",
34
+                    "type": "Value",
35
+                    "id": "eb19afaa-83cb-48ec-8718-af2ea48a9bc8",
36
+                    "expectedControlType": "Button",
37
+                    "processors": "",
38
+                    "interactions": ""
39
+                },
40
+                {
41
+                    "name": "Sprint",
42
+                    "type": "Value",
43
+                    "id": "ad65292f-dcff-4d57-b698-482297345782",
44
+                    "expectedControlType": "Button",
45
+                    "processors": "",
46
+                    "interactions": ""
23 47
                 }
24 48
             ],
25 49
             "bindings": [
@@ -79,6 +103,61 @@
79 103
                     "isPartOfComposite": true
80 104
                 },
81 105
                 {
106
+                    "name": "WASD",
107
+                    "id": "437251c2-ab5e-4ecd-9da0-054532ba3060",
108
+                    "path": "2DVector",
109
+                    "interactions": "",
110
+                    "processors": "",
111
+                    "groups": "",
112
+                    "action": "Movement",
113
+                    "isComposite": true,
114
+                    "isPartOfComposite": false
115
+                },
116
+                {
117
+                    "name": "up",
118
+                    "id": "b69f7258-14ce-4041-aba3-8c569de69dc7",
119
+                    "path": "<Keyboard>/w",
120
+                    "interactions": "",
121
+                    "processors": "",
122
+                    "groups": "",
123
+                    "action": "Movement",
124
+                    "isComposite": false,
125
+                    "isPartOfComposite": true
126
+                },
127
+                {
128
+                    "name": "down",
129
+                    "id": "65c57866-904c-45d0-b16c-1454bd2597e2",
130
+                    "path": "<Keyboard>/s",
131
+                    "interactions": "",
132
+                    "processors": "",
133
+                    "groups": "",
134
+                    "action": "Movement",
135
+                    "isComposite": false,
136
+                    "isPartOfComposite": true
137
+                },
138
+                {
139
+                    "name": "left",
140
+                    "id": "c61cc306-727b-4470-b581-d2c472ada66e",
141
+                    "path": "<Keyboard>/a",
142
+                    "interactions": "",
143
+                    "processors": "",
144
+                    "groups": "",
145
+                    "action": "Movement",
146
+                    "isComposite": false,
147
+                    "isPartOfComposite": true
148
+                },
149
+                {
150
+                    "name": "right",
151
+                    "id": "0813b56f-d53f-4bd1-bae8-2e7cfb4f644f",
152
+                    "path": "<Keyboard>/d",
153
+                    "interactions": "",
154
+                    "processors": "",
155
+                    "groups": "",
156
+                    "action": "Movement",
157
+                    "isComposite": false,
158
+                    "isPartOfComposite": true
159
+                },
160
+                {
82 161
                     "name": "",
83 162
                     "id": "b00386e1-8b81-4b23-add0-14dfd7c1fc48",
84 163
                     "path": "<Mouse>/delta",
@@ -88,6 +167,39 @@
88 167
                     "action": "Look",
89 168
                     "isComposite": false,
90 169
                     "isPartOfComposite": false
170
+                },
171
+                {
172
+                    "name": "",
173
+                    "id": "a629ccdb-feec-4006-99da-ecd1fc2a582a",
174
+                    "path": "<Mouse>/leftButton",
175
+                    "interactions": "",
176
+                    "processors": "",
177
+                    "groups": "",
178
+                    "action": "Place Block",
179
+                    "isComposite": false,
180
+                    "isPartOfComposite": false
181
+                },
182
+                {
183
+                    "name": "",
184
+                    "id": "8f6ba03e-dbdb-42ce-be6f-5550c0a2a444",
185
+                    "path": "<Mouse>/rightButton",
186
+                    "interactions": "",
187
+                    "processors": "",
188
+                    "groups": "",
189
+                    "action": "Toggle Preview Mode",
190
+                    "isComposite": false,
191
+                    "isPartOfComposite": false
192
+                },
193
+                {
194
+                    "name": "",
195
+                    "id": "171894bd-9d65-472e-8e83-7cf79f2057ab",
196
+                    "path": "<Keyboard>/leftShift",
197
+                    "interactions": "",
198
+                    "processors": "",
199
+                    "groups": "",
200
+                    "action": "Sprint",
201
+                    "isComposite": false,
202
+                    "isPartOfComposite": false
91 203
                 }
92 204
             ]
93 205
         }

+ 78
- 0
FreeCam/Assets/Materials/New Material.mat Прегледај датотеку

@@ -0,0 +1,78 @@
1
+%YAML 1.1
2
+%TAG !u! tag:unity3d.com,2011:
3
+--- !u!21 &2100000
4
+Material:
5
+  serializedVersion: 6
6
+  m_ObjectHideFlags: 0
7
+  m_CorrespondingSourceObject: {fileID: 0}
8
+  m_PrefabInstance: {fileID: 0}
9
+  m_PrefabAsset: {fileID: 0}
10
+  m_Name: New Material
11
+  m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
12
+  m_ShaderKeywords: 
13
+  m_LightmapFlags: 4
14
+  m_EnableInstancingVariants: 0
15
+  m_DoubleSidedGI: 0
16
+  m_CustomRenderQueue: -1
17
+  stringTagMap: {}
18
+  disabledShaderPasses: []
19
+  m_SavedProperties:
20
+    serializedVersion: 3
21
+    m_TexEnvs:
22
+    - _BumpMap:
23
+        m_Texture: {fileID: 0}
24
+        m_Scale: {x: 1, y: 1}
25
+        m_Offset: {x: 0, y: 0}
26
+    - _DetailAlbedoMap:
27
+        m_Texture: {fileID: 0}
28
+        m_Scale: {x: 1, y: 1}
29
+        m_Offset: {x: 0, y: 0}
30
+    - _DetailMask:
31
+        m_Texture: {fileID: 0}
32
+        m_Scale: {x: 1, y: 1}
33
+        m_Offset: {x: 0, y: 0}
34
+    - _DetailNormalMap:
35
+        m_Texture: {fileID: 0}
36
+        m_Scale: {x: 1, y: 1}
37
+        m_Offset: {x: 0, y: 0}
38
+    - _EmissionMap:
39
+        m_Texture: {fileID: 0}
40
+        m_Scale: {x: 0.85, y: 0.85}
41
+        m_Offset: {x: 0.075, y: 0.075}
42
+    - _MainTex:
43
+        m_Texture: {fileID: 10906, guid: 0000000000000000f000000000000000, type: 0}
44
+        m_Scale: {x: 0.85, y: 0.85}
45
+        m_Offset: {x: 0.075, y: 0.075}
46
+    - _MetallicGlossMap:
47
+        m_Texture: {fileID: 0}
48
+        m_Scale: {x: 1, y: 1}
49
+        m_Offset: {x: 0, y: 0}
50
+    - _OcclusionMap:
51
+        m_Texture: {fileID: 0}
52
+        m_Scale: {x: 1, y: 1}
53
+        m_Offset: {x: 0, y: 0}
54
+    - _ParallaxMap:
55
+        m_Texture: {fileID: 0}
56
+        m_Scale: {x: 1, y: 1}
57
+        m_Offset: {x: 0, y: 0}
58
+    m_Floats:
59
+    - _BumpScale: 1
60
+    - _Cutoff: 0.5
61
+    - _DetailNormalMapScale: 1
62
+    - _DstBlend: 0
63
+    - _GlossMapScale: 1
64
+    - _Glossiness: 0.686
65
+    - _GlossyReflections: 1
66
+    - _Metallic: 0.5
67
+    - _Mode: 0
68
+    - _OcclusionStrength: 1
69
+    - _Parallax: 0.02
70
+    - _SmoothnessTextureChannel: 0
71
+    - _SpecularHighlights: 1
72
+    - _SrcBlend: 1
73
+    - _UVSec: 0
74
+    - _ZWrite: 1
75
+    m_Colors:
76
+    - _Color: {r: 0.42955682, g: 0.4301132, b: 0.46226418, a: 1}
77
+    - _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
78
+  m_BuildTextureStacks: []

+ 8
- 0
FreeCam/Assets/Materials/New Material.mat.meta Прегледај датотеку

@@ -0,0 +1,8 @@
1
+fileFormatVersion: 2
2
+guid: 7a1df68ff2cb2814088ca3c160bfdf17
3
+NativeFormatImporter:
4
+  externalObjects: {}
5
+  mainObjectFileID: 0
6
+  userData: 
7
+  assetBundleName: 
8
+  assetBundleVariant: 

+ 15
- 0
FreeCam/Assets/Player.prefab Прегледај датотеку

@@ -258,6 +258,7 @@ GameObject:
258 258
   - component: {fileID: 562755809799988217}
259 259
   - component: {fileID: 562755809799988216}
260 260
   - component: {fileID: 5739138180788424124}
261
+  - component: {fileID: 6193258497320629589}
261 262
   m_Layer: 0
262 263
   m_Name: Player
263 264
   m_TagString: Untagged
@@ -346,3 +347,17 @@ CharacterController:
346 347
   m_SkinWidth: 0.08
347 348
   m_MinMoveDistance: 0.001
348 349
   m_Center: {x: 0, y: 0, z: 0}
350
+--- !u!114 &6193258497320629589
351
+MonoBehaviour:
352
+  m_ObjectHideFlags: 0
353
+  m_CorrespondingSourceObject: {fileID: 0}
354
+  m_PrefabInstance: {fileID: 0}
355
+  m_PrefabAsset: {fileID: 0}
356
+  m_GameObject: {fileID: 562755809799988215}
357
+  m_Enabled: 1
358
+  m_EditorHideFlags: 0
359
+  m_Script: {fileID: 11500000, guid: b78dfde35e58d1e4c858653eaf06ea64, type: 3}
360
+  m_Name: 
361
+  m_EditorClassIdentifier: 
362
+  blockToPlace: {fileID: 3300812737117657924, guid: 7b30a4c90d614f747872471cf43b8e56, type: 3}
363
+  spawnDistance: 3

+ 4
- 0
FreeCam/Assets/Scenes/SampleScene.unity Прегледај датотеку

@@ -684,5 +684,9 @@ PrefabInstance:
684 684
       propertyPath: m_Name
685 685
       value: Player
686 686
       objectReference: {fileID: 0}
687
+    - target: {fileID: 6193258497320629589, guid: 37333bbc22b455449a05fcf8aa6b9a8f, type: 3}
688
+      propertyPath: _blockToPlace
689
+      value: 
690
+      objectReference: {fileID: 0}
687 691
     m_RemovedComponents: []
688 692
   m_SourcePrefab: {fileID: 100100000, guid: 37333bbc22b455449a05fcf8aa6b9a8f, type: 3}

+ 12
- 2
FreeCam/Assets/Scripts/FreeCam.cs Прегледај датотеку

@@ -6,6 +6,7 @@ public class FreeCam : MonoBehaviour
6 6
     [Header("Controls")]
7 7
 
8 8
     public float moveSpeed = 10f;
9
+    public float sprintSpeed = 30f;
9 10
     public bool ghost = true;
10 11
 
11 12
     [Header("Sensitivity")]
@@ -24,6 +25,7 @@ public class FreeCam : MonoBehaviour
24 25
     private CharacterController _controller;
25 26
 
26 27
     private float _rotationX;
28
+    private bool _isSprinting;
27 29
 
28 30
 
29 31
     private void Start()
@@ -37,6 +39,7 @@ public class FreeCam : MonoBehaviour
37 39
         _controller = gameObject.GetComponent<CharacterController>();
38 40
 
39 41
         _rotationX = 0;
42
+        _isSprinting = false;
40 43
     }
41 44
 
42 45
     // Update is called once per frame
@@ -56,8 +59,9 @@ public class FreeCam : MonoBehaviour
56 59
 
57 60
         transform.localEulerAngles = new Vector3(-_rotationX, rotationY, 0);
58 61
 
59
-        float translationX = _velocity.x * moveSpeed * Time.deltaTime;
60
-        float translationZ = _velocity.y * moveSpeed * Time.deltaTime;
62
+        float speed = _isSprinting ? sprintSpeed : moveSpeed;
63
+        float translationX = _velocity.x * speed * Time.deltaTime;
64
+        float translationZ = _velocity.y * speed * Time.deltaTime;
61 65
 
62 66
         Vector3 _move = new Vector3(translationX, 0f, translationZ);
63 67
 
@@ -83,4 +87,10 @@ public class FreeCam : MonoBehaviour
83 87
         _velocity.x = valueAsVector2.x;
84 88
         _velocity.y = valueAsVector2.y;
85 89
     }
90
+
91
+    public void OnSprint(InputValue value)
92
+    {
93
+        float valueAsFloat = value.Get<float>();
94
+        _isSprinting = valueAsFloat == 1f;
95
+    }
86 96
 }

Loading…
Откажи
Сачувај