]> git.cameronkatri.com Git - gmtk-gamejam.git/commitdiff
Physics in FixedUpdate
authorCameron Katri <me@cameronkatri.com>
Sat, 12 Jun 2021 18:59:41 +0000 (14:59 -0400)
committerCameron Katri <me@cameronkatri.com>
Sat, 12 Jun 2021 18:59:41 +0000 (14:59 -0400)
Assets/Prefabs/Player.prefab
Assets/Scenes/Main.unity
Assets/Scripts/Controllers/Player/PlayerController.cs
Assets/Scripts/Controllers/Player/PlayerSegment.cs

index 521b4bf375113ad37a5fc7b1c5d3c442b0ab460a..a7aaff72574e08f7001b87bc6fff58d0fbbb2ade 100644 (file)
@@ -1132,6 +1132,8 @@ MonoBehaviour:
     arm: {fileID: 8845918641001187994}
   forceOrigin: {x: 0, y: 0.5, z: 0}
   height: 0
+  isRoot: 0
+  isTop: 1
 --- !u!50 &6095859832867562452
 Rigidbody2D:
   serializedVersion: 4
@@ -1424,6 +1426,8 @@ MonoBehaviour:
     arm: {fileID: 61032117990659215}
   forceOrigin: {x: 0, y: -0.5, z: 0}
   height: 0
+  isRoot: 1
+  isTop: 0
 --- !u!50 &6095859833589646292
 Rigidbody2D:
   serializedVersion: 4
index d003365a0b195309b01d06c48b8232053c4e9f63..89790712e3cf667cafcaaf8cf3241a5a6a393ca4 100644 (file)
@@ -353,6 +353,10 @@ PrefabInstance:
       propertyPath: m_LocalEulerAnglesHint.z
       value: 0
       objectReference: {fileID: 0}
+    - target: {fileID: 6267610020350316690, guid: 2485406c33a7d4f5481ce936cc9b162e, type: 3}
+      propertyPath: shotForce
+      value: 1000
+      objectReference: {fileID: 0}
     m_RemovedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: 2485406c33a7d4f5481ce936cc9b162e, type: 3}
 --- !u!114 &707826158 stripped
@@ -431,7 +435,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 6095859834264611326, guid: c64ad9b908cb44602b3d13a74aef751f, type: 3}
       propertyPath: sensitivity
-      value: 2
+      value: 10
       objectReference: {fileID: 0}
     - target: {fileID: 6095859834264611326, guid: c64ad9b908cb44602b3d13a74aef751f, type: 3}
       propertyPath: upSensitivity
@@ -439,7 +443,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 6095859834264611326, guid: c64ad9b908cb44602b3d13a74aef751f, type: 3}
       propertyPath: verticalSensitivity
-      value: 1.5
+      value: 7.5
       objectReference: {fileID: 0}
     m_RemovedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: c64ad9b908cb44602b3d13a74aef751f, type: 3}
index eca46413b7df5994a12e971dcf5decb935020d83..3674d92cc22c67a8720dd317f60f77eee0333adb 100644 (file)
@@ -17,15 +17,21 @@ namespace Controllers.Player
 
     [SerializeField] private float maxLegAngle;
     [SerializeField] private float legSpeed;
-    
+
+    private void Awake()
+    {
+      foreach (var segment in segments) {
+        segment.sensitivity = sensitivity;
+        segment.verticalSensitivity = verticalSensitivity;
+      }
+    }
+
     private void Update()
     {
       // Loop over each body segment and control it.
       for (var i = 0; i < segments.Length; i++)
       {
         var segment = segments[i];
-        var isRoot = i == 0;
-        var isTop = i == segments.Length - 1;
         
         // Update icons
         foreach (var control in new[] {segment.left, segment.right})
@@ -37,24 +43,11 @@ namespace Controllers.Player
         }
 
         // Add forces
-        if (Input.GetKey(segment.left.keyCode))
-        {
-          segment.Rigidbody.AddForceAtPosition(
-            Vector2.left * (sensitivity * (isRoot ? 2f : 1f)) +
-            Vector2.up * (verticalSensitivity * (isRoot ? -1f : 1f)),
-            segment.transform.position + (segment.height * segment.forceOrigin));
-        }
-
-        if (Input.GetKey(segment.right.keyCode))
-        {
-          segment.Rigidbody.AddForceAtPosition(
-            Vector2.right * (sensitivity * (isRoot ? 2f : 1f)) +
-            Vector2.up * (verticalSensitivity * (isRoot ? -1f : 1f)),
-            segment.transform.position + (segment.height * segment.forceOrigin));
-        }
+        segment.moveleft = Input.GetKey(segment.left.keyCode);
+        segment.moveright = Input.GetKey(segment.right.keyCode);
         
         // Rotate the legs if we move.
-        if (isRoot)
+        if (segment.isRoot)
         {
           if (Input.GetKey(segment.left.keyCode) || Input.GetKey(segment.right.keyCode))
           {
@@ -70,7 +63,7 @@ namespace Controllers.Player
           }
         }
 
-        if (isTop)
+        if (segment.isTop)
         {
           game.player.Move(segment.transform.position);
           
index b713442b5205cf4a83e092d8dadf7e27bb6fd0bc..4d0fb4f44a66ce541a628be4441015c768ff0cc8 100644 (file)
@@ -6,15 +6,39 @@ namespace Controllers.Player
   [RequireComponent(typeof(Rigidbody2D))]
   public class PlayerSegment : MonoBehaviour
   {
+    internal float sensitivity;
+    internal float verticalSensitivity;
+    
+    internal bool moveleft;
+    internal bool moveright;
+    
     [SerializeField] public Control left;
     [SerializeField] public Control right;
 
     [SerializeField] public Vector3 forceOrigin;
 
     [SerializeField] public float height;
+    [SerializeField] public bool isRoot;
+    [SerializeField] public bool isTop;
 
     public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent<Rigidbody2D>();
     private Rigidbody2D _rigidbody;
+
+    private void FixedUpdate()
+    {
+      if (moveleft)
+      {
+        Rigidbody.AddForceAtPosition(
+          Vector2.left * (sensitivity * (isRoot ? 2f : 1f)) + Vector2.up * (verticalSensitivity * (isRoot ? -1f : 1f)),
+          transform.position + (height * forceOrigin));
+      }
+      if (moveright)
+      {
+        Rigidbody.AddForceAtPosition(
+          Vector2.right * (sensitivity * (isRoot ? 2f : 1f)) + Vector2.up * (verticalSensitivity * (isRoot ? -1f : 1f)),
+          transform.position + (height * forceOrigin));
+      }
+    }
   }
 
   [Serializable]