Path-Creator
Path-Creator copied to clipboard
How to make Rigid body follow a path?
It would be grate to have tutorial on HOW TO MAKE RIGID BODY FOLLOW BESIER PATH. It's hard for me to do it.
You can use something like that
private void FixedUpdate()
{
PathComplete += _speed * Time.fixedDeltaTime;
if (PathComplete > PathLength)
{
GameController.Instance.LevelComplete();
_rigidbody.velocity = Vector3.zero;
enabled = false;
return;
}
var rotation = _pathCreator.path.GetRotationAtDistance(PathComplete, EndOfPathInstruction.Stop);
var destination = _pathCreator.path.GetPointAtDistance(PathComplete, EndOfPathInstruction.Stop);
destination += Quaternion.Euler(0f, 90f, 0f) * rotation * Vector3.forward * _pathOffset;
var velocity = (destination - transform.position) / Time.fixedDeltaTime;
_rigidbody.velocity = Vector3.MoveTowards(_rigidbody.velocity, velocity, _velocityChangeSpeed * Time.fixedDeltaTime);
transform.rotation = rotation;
}