38 lines
1.0 KiB
C#
38 lines
1.0 KiB
C#
using UnityEngine;
|
|
|
|
public class MovingSphere : MonoBehaviour
|
|
{
|
|
[SerializeField, Range(0f, 100f)]
|
|
private float maxSpeed = 10f;
|
|
|
|
[SerializeField, Range(0f, 100f)]
|
|
private float maxAcceleration = 10f;
|
|
|
|
private Rigidbody body;
|
|
|
|
private Vector3 desiredVelocity;
|
|
|
|
private void Awake()
|
|
{
|
|
body = GetComponent<Rigidbody>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
Vector2 playerInput;
|
|
playerInput.x = Input.GetAxis("Horizontal");
|
|
playerInput.y = Input.GetAxis("Vertical");
|
|
playerInput = Vector2.ClampMagnitude(playerInput, 1f);
|
|
desiredVelocity = new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
var velocity = body.velocity;
|
|
float maxSpeedChange = maxAcceleration * Time.deltaTime;
|
|
velocity.x = Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
|
|
velocity.z = Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);
|
|
body.velocity = velocity;
|
|
}
|
|
}
|