diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 3ed2285..ca5a89b 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -626,3 +626,4 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 50beb4685c906ba49b7db3438cc30dfc, type: 3} m_Name: m_EditorClassIdentifier: + maxSpeed: 10 diff --git a/Assets/Scripts/MovingSphere.cs b/Assets/Scripts/MovingSphere.cs index 7a52f7c..f105322 100644 --- a/Assets/Scripts/MovingSphere.cs +++ b/Assets/Scripts/MovingSphere.cs @@ -2,12 +2,25 @@ public class MovingSphere : MonoBehaviour { + [SerializeField, Range(0f, 100f)] + private float maxSpeed = 10f; + + [SerializeField, Range(0f, 100f)] + private float maxAcceleration = 10f; + + private Vector3 velocity; + private void Update() { Vector2 playerInput; playerInput.x = Input.GetAxis("Horizontal"); playerInput.y = Input.GetAxis("Vertical"); playerInput = Vector2.ClampMagnitude(playerInput, 1f); - transform.localPosition = new Vector3(playerInput.x, 0.5f, playerInput.y); + var desiredVelocity = new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed; + float maxSpeedChange = maxAcceleration * Time.deltaTime; + velocity.x = Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange); + velocity.z = Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange); + var displacement = velocity * Time.deltaTime; + transform.localPosition += displacement; } }