A
A
Ark Tarusov2018-02-04 08:49:16
Unity
Ark Tarusov, 2018-02-04 08:49:16

Cell movement with RigidBody?

Good afternoon!
It is necessary that the character moves through the cells and physics is not violated.
That is, they pressed the button to the right, he moved 1 cell to the right.
If he ran into something, he did not move.
Whether he can push the object or rest against it is decided by physics itself.
If you move an object through a transform, then physics does not work.
If you manually lock the buttons when moving and check if the player has reached the next cell, then when he rests, the buttons will not unlock.
Does anyone know a suitable way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ark Tarusov, 2018-02-05
@kreo_OL

Did it in the end

private IEnumerator Move(Axis axis, int point)
    {
        var direction = Vector3.zero;
        direction[(int) axis] = point;
        _move = true;
        if (Physics.RaycastAll(transform.position, direction, 2f).Length > 1)
        {
            _move = false;
            yield break;
        }
        _finishPosition = transform.position + direction;
        _timer = 0;
        _curve = new AnimationCurve();
        _curve.AddKey(0f, transform.position[(int) axis]);
        _curve.AddKey(_speedMove, _finishPosition[(int) axis]);
        while (true)
        {
            _tmpPosition = transform.position;
            _tmpPosition[(int) axis] = _curve.Evaluate(_timer);
            _rb.MovePosition(_tmpPosition);
            if (transform.position == _finishPosition)
            {
                _move = false;
                yield break;
            }
            _timer += Time.fixedDeltaTime;
            yield return new WaitForFixedUpdate();
        }
    }

axis is an enum with axes.
There is a problem, sometimes it does not exit the loop when the point is reached. However, the transform gives the desired point.
5a78638695251628418688.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question