How can I Stop rigidbody object moving by rigidbody.moveposition?

public managerSwipe swipeControl;
public Transform player;
public Rigidbody playerRb;
private Vector3 playerPosition;

    public float speed = 0.6f;

    private void Start()
    {
        playerRb = player.GetComponent<Rigidbody>();
    }

    // only for RigidBody added object
    private void FixedUpdate()
    {
        if (swipeControl.SwipeLeft)
            playerPosition += Vector3.left * 2;
        if (swipeControl.SwipeRight)
            playerPosition += Vector3.right * 2;
        if (swipeControl.SwipeUp)
            playerPosition += Vector3.forward * 2;
        if (swipeControl.SwipeDown)
            playerPosition += Vector3.back * 2;

        playerRb.MovePosition(player.transform.position + playerPosition * speed * Time.deltaTime);

    }

hello, I’m learning UNITY in Korea, and I would like to make ‘Speed Ball’ like game.
Swiping code for moving the object works fine, but the object is moving in endless.

If I want to move the object ‘1 meter’ by each swipe, how can I stop the object?

Please help me :frowning:

As i see it now, you are adding to the playerPosition but you are not setting it back to zero. This will mean that the playerPosition will keep growing and growing. In combination with that, you are calling .MovePosition every fixed frame. So that will mean that it will keep adding the playerPosition to the players position every frame. I’m not really sure how your game works, but you either want to check the position of the player and see if it reached the target and then setting the playerPosition to zero OR put the .MovePosition in the swipeControl. if statement so it only moves when you swipe.

@metalted Thank you for reply. I added some codes and the object is stopped, How I stop the object is, checking the object’s position (maybe world position?) and when it move 1 or -1, the code makes the object stop.
The problem is, stopped object is not moving again after I swiped in any directions.
If it is my fault or not, [velocity = Vector3.zero;] not worked, When I make the ‘speed’ zero, it stopped.
I think this is not a perfect way, because the object is still trying to move although the speed is ‘zero’.
[ Debug.Log (“Stop”); is calling endlessly.]

Here is my modified code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class managerSwipeMovePlayer : MonoBehaviour {

    public managerSwipe swipeControl;
    public Transform player;
    public Rigidbody playerRb;
    private Vector3 playerPosition;

    public float speed = 0.6f;
    float speedMove = 0.6f;
    float speedStop = 0f;

    private void Start()
    {
        playerRb = player.GetComponent<Rigidbody>();
    }

    // only for RigidBody added object
    private void FixedUpdate()
    {
        if (swipeControl.SwipeLeft)
        {
            playerPosition += Vector3.left * 2;
            MoveStart();
        }
            
        if (swipeControl.SwipeRight)
        {
            playerPosition += Vector3.right * 2;
            MoveStart();
        }

        if (swipeControl.SwipeUp)
        {
            playerPosition += Vector3.forward * 2;
            MoveStart();
        }
            
        if (swipeControl.SwipeDown)
        {
            playerPosition += Vector3.back * 2;
            MoveStart();
        }
            

        //Vector3 towardsOther = (player.transform.position - transform.position);
        //Debug.Log(transform.position);
        playerRb.MovePosition(player.transform.position + playerPosition * speed * Time.deltaTime);

        if (player.transform.position.x >= 1 || player.transform.position.x <= -1)
        {
            Debug.Log("Stop");
            stopMove();
            //speed = 0;
            //playerRb.velocity = Vector3.zero;
        }

        //playerRb.velocity = playerPosition * speed * Time.deltaTime;

    }

    void stopMove()
    {
        speed = speedStop;
        playerRb.velocity = Vector3.zero;
    }

    void MoveStart()
    {
        speed = speedMove;
    }



}