snap to grid

This commit is contained in:
klemek
2020-12-11 17:15:37 +01:00
parent 4bc26a2439
commit e43a0c6de0
6 changed files with 103 additions and 8 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1824742176375353076}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.5, y: 0.5, z: 0}
m_LocalPosition: {x: 0.5, y: 0.5, z: -1}
m_LocalScale: {x: 0.5, y: 0.5, z: 1}
m_Children: []
m_Father: {fileID: 0}
+1 -1
View File
@@ -26,7 +26,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2180376356050932351}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalPosition: {x: 0, y: 0, z: -0.5}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
+83 -3
View File
@@ -32,6 +32,7 @@ Transform:
- {fileID: 118974315500811181}
- {fileID: 555726995092159938}
- {fileID: 120012632069647147}
- {fileID: 1658337153186624383}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -139,6 +140,85 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 419a02bb4cfe59848a1f7677a1018aa3, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &5358625439717083291
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1658337153186624383}
- component: {fileID: 5623837103916873807}
- component: {fileID: 2169103976044701791}
m_Layer: 0
m_Name: box
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1658337153186624383
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5358625439717083291}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 3, y: -2.5, z: 1}
m_LocalScale: {x: 5.5, y: 4.5, z: 1}
m_Children: []
m_Father: {fileID: 2070848596161447923}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!61 &5623837103916873807
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5358625439717083291}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 1
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
oldSize: {x: 0, y: 0}
newSize: {x: 0, y: 0}
adaptiveTilingThreshold: 0
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 1, y: 1}
m_EdgeRadius: 0
--- !u!50 &2169103976044701791
Rigidbody2D:
serializedVersion: 4
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5358625439717083291}
m_BodyType: 1
m_Simulated: 1
m_UseFullKinematicContacts: 0
m_UseAutoMass: 0
m_Mass: 1
m_LinearDrag: 0
m_AngularDrag: 0.05
m_GravityScale: 0
m_Material: {fileID: 0}
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_Constraints: 0
--- !u!1001 &14861468116553427
PrefabInstance:
m_ObjectHideFlags: 0
@@ -251,7 +331,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3}
propertyPath: m_LocalPosition.z
value: 0
value: -1
objectReference: {fileID: 0}
- target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3}
propertyPath: m_LocalRotation.w
@@ -321,7 +401,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3}
propertyPath: m_LocalPosition.z
value: 0
value: -1
objectReference: {fileID: 0}
- target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3}
propertyPath: m_LocalRotation.w
@@ -395,7 +475,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3}
propertyPath: m_LocalPosition.z
value: 0
value: -1
objectReference: {fileID: 0}
- target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3}
propertyPath: m_LocalRotation.w
+12 -2
View File
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UntitledLogicGame
@@ -20,6 +21,7 @@ namespace UntitledLogicGame
private Cable _currentCable;
private Gate _currentGate;
private Vector3 _currentGateInitialPos;
private Vector3 _currentGateDelta;
#endregion
@@ -31,7 +33,7 @@ namespace UntitledLogicGame
}
private void Update()
private void FixedUpdate()
{
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0f;
@@ -54,7 +56,8 @@ namespace UntitledLogicGame
{
_currentGate = GameManager.Instance.CurrentGate;
_currentGateDelta = MousePos - _currentGate.transform.position;
foreach(var renderer in _currentGate.GetComponentsInChildren<SpriteRenderer>())
_currentGateInitialPos = _currentGate.transform.position;
foreach (var renderer in _currentGate.GetComponentsInChildren<SpriteRenderer>())
{
renderer.sortingLayerName = "moving";
}
@@ -79,6 +82,13 @@ namespace UntitledLogicGame
{
renderer.sortingLayerName = "default";
}
_currentGate.transform.position = _currentGate.transform.position.Round();
var currentBox = _currentGate.GetComponentInChildren<BoxCollider2D>();
if (FindObjectsOfType<Gate>()
.Where(g => !g.Equals(_currentGate))
.Select(g => g.GetComponentInChildren<BoxCollider2D>())
.Any(b => currentBox.IsTouching(b))) // Collision with another gate
_currentGate.transform.position = _currentGateInitialPos; // Reset pos
_currentGate = null;
}
}
+5
View File
@@ -51,5 +51,10 @@ namespace UntitledLogicGame
{
return array.Select((v, i) => (v ? 1 : 0) << (array.Length - i - 1)).Sum();
}
public static Vector3 Round(this Vector3 v)
{
return new Vector3(Mathf.Round(v.x), Mathf.Round(v.y), Mathf.Round(v.z));
}
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ TagManager:
-
-
- Background Image
-
- test
-
-
-