Snippets

Kelly Thomas TestMovement

Updated by Former user

File TestMovement Modified

  • Ignore whitespace
  • Hide word diff
 {
 
     public float bounds = 5f;
-    public float position= -5;
+    public float position = -5;
     public float velocity;
     public float velocityMax = 5f;
     public float accelerationRate = 100f;
     public float accelerationDirection = 1f;
-    
+
     public bool useKinematics = false;
 
     void Start()
         Debug.Log(string.Format("{0} bounces on the {1} at {2}", useKinematics ? "Kinematics" : "Traditional", side, Time.time));
     }
 
-	void Update ()
-	{
+    void Update()
+    {
         // clamp to bounds
         if (position > bounds && accelerationDirection > 0)
-	    {
-	        accelerationDirection = -1;
+        {
+            accelerationDirection = -1;
             position = bounds;
             LogBounce("right");
-	    }
+        }
         else if (position < -bounds && accelerationDirection < 0)
-	    {
+        {
             accelerationDirection = 1;
             position = -bounds;
             LogBounce("left");
-	    }
+        }
 
-	    float acceleration = accelerationRate * accelerationDirection;
+        float acceleration = accelerationRate * accelerationDirection;
 
         if (useKinematics)
-	    {
-	        velocity += acceleration * Time.deltaTime;
+        {
+            velocity += acceleration * Time.deltaTime;
             velocity = Mathf.Clamp(velocity, -velocityMax, velocityMax);
-	        position += velocity * Time.deltaTime;
-	    }
-	    else
-	    {
-	        position += (velocity * Time.deltaTime) + (acceleration * Time.deltaTime * Time.deltaTime / 2);
-	        velocity += acceleration * Time.deltaTime;
+            position += velocity * Time.deltaTime;
+        }
+        else
+        {
+            position += (velocity * Time.deltaTime) + (acceleration * Time.deltaTime * Time.deltaTime / 2);
+            velocity += acceleration * Time.deltaTime;
             velocity = Mathf.Clamp(velocity, -velocityMax, velocityMax);
-	    }
+        }
 
-	    Vector3 pos = transform.position;
-	    pos.x = position;
-	    transform.position = pos;
-	}
+        Vector3 pos = transform.position;
+        pos.x = position;
+        transform.position = pos;
+    }
 }
Created by Former user

File TestMovement Added

  • Ignore whitespace
  • Hide word diff
+using UnityEngine;
+
+public class TestMovement : MonoBehaviour
+{
+
+    public float bounds = 5f;
+    public float position= -5;
+    public float velocity;
+    public float velocityMax = 5f;
+    public float accelerationRate = 100f;
+    public float accelerationDirection = 1f;
+    
+    public bool useKinematics = false;
+
+    void Start()
+    {
+        int fps = 60;
+        Application.targetFrameRate = fps;
+        QualitySettings.vSyncCount = 0;
+        Debug.Log(string.Format("Starting simulation with targetFrameRate of {0}", fps));
+    }
+
+    void LogBounce(string side)
+    {
+        Debug.Log(string.Format("{0} bounces on the {1} at {2}", useKinematics ? "Kinematics" : "Traditional", side, Time.time));
+    }
+
+	void Update ()
+	{
+        // clamp to bounds
+        if (position > bounds && accelerationDirection > 0)
+	    {
+	        accelerationDirection = -1;
+            position = bounds;
+            LogBounce("right");
+	    }
+        else if (position < -bounds && accelerationDirection < 0)
+	    {
+            accelerationDirection = 1;
+            position = -bounds;
+            LogBounce("left");
+	    }
+
+	    float acceleration = accelerationRate * accelerationDirection;
+
+        if (useKinematics)
+	    {
+	        velocity += acceleration * Time.deltaTime;
+            velocity = Mathf.Clamp(velocity, -velocityMax, velocityMax);
+	        position += velocity * Time.deltaTime;
+	    }
+	    else
+	    {
+	        position += (velocity * Time.deltaTime) + (acceleration * Time.deltaTime * Time.deltaTime / 2);
+	        velocity += acceleration * Time.deltaTime;
+            velocity = Mathf.Clamp(velocity, -velocityMax, velocityMax);
+	    }
+
+	    Vector3 pos = transform.position;
+	    pos.x = position;
+	    transform.position = pos;
+	}
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.