You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
1.1 KiB
C#

using UnityEngine;
namespace Class03
{
// 입력 방향에 따라 Rigidbody에 힘을 가하는 역할만 합니다.
[RequireComponent(typeof(Rigidbody), typeof(KeyboardMoveInput))]
public sealed class BallMotor : MonoBehaviour
{
[SerializeField] private float acceleration = 18f;
[SerializeField] private float maximumSpeed = 7f;
private Rigidbody body;
private KeyboardMoveInput input;
private void Awake()
{
body = GetComponent<Rigidbody>();
input = GetComponent<KeyboardMoveInput>();
}
private void FixedUpdate()
{
Vector2 direction = input.Direction;
body.AddForce(new Vector3(direction.x, 0f, direction.y) * acceleration,
ForceMode.Acceleration);
Vector3 velocity = body.linearVelocity;
Vector3 horizontal = Vector3.ClampMagnitude(new Vector3(velocity.x, 0f, velocity.z), maximumSpeed);
body.linearVelocity = new Vector3(horizontal.x, velocity.y, horizontal.z);
}
}
}