Path-Creator icon indicating copy to clipboard operation
Path-Creator copied to clipboard

How to make Rigid body follow a path?

Open FrontKolia opened this issue 4 years ago • 1 comments

It would be grate to have tutorial on HOW TO MAKE RIGID BODY FOLLOW BESIER PATH. It's hard for me to do it.

FrontKolia avatar Jul 28 '20 11:07 FrontKolia

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;
    }

LeonHromyko avatar Jul 28 '20 18:07 LeonHromyko