Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Trying to move my object in another object's position

Discussion in 'Physics' started by AlexFeliz, Aug 30, 2023.

  1. AlexFeliz

    AlexFeliz

    Joined:
    Jun 12, 2023
    Posts:
    3
    For some reason player doesn't move in the middle of the block, but stops when reaches the side of the block.
    FindBlockToMove() is just a spot, but it has a child visual object with a collider. player is also just a spot and it has a child visual object. Visual objects of player and block are not touching.
    Why can it happen and what should I do to make player move in the middle of the block?
    And it happens both with MoveTowards and with Lerp


    private void MoveToChosenBlock()
    {
    GameObject _blockToMove = FindBlockToMove();
    Debug.Log($"Now we move to {_blockToMove}");
    float _moveDistance = _moveSpeed * Time.deltaTime;
    Vector3 _blockPosition = FindBlockToMove().transform.position;

    transform.position = Vector3.MoveTowards(transform.position, _blockPosition, _moveDistance);
    //transform.position = Vector3.Lerp(transform.position, _blockPosition, 0.1f);
    }
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,978
    This is a scripting issue. Since you're using
    transform.position
    , it's not a physics question at all. Maybe a mod will move this thread appropriately. And next time, use the code tags to format your scripts here.


    All of your code looks fine for moving at a set speed toward the destination, but you will have to call MoveToChosenBlock() on every Update() until it reaches the chosen block.
     
    AlexFeliz likes this.
  3. AlexFeliz

    AlexFeliz

    Joined:
    Jun 12, 2023
    Posts:
    3
    Thank you! This is my first post here, so I was a lil bit confused...

    I do call this method in Update, and player moves. But it stops when it touches the side of a visual part of block and I want it to move to the block's position which is in the middle of it's visual part. I see, it's hard to explain without a video
     
  4. AlexFeliz

    AlexFeliz

    Joined:
    Jun 12, 2023
    Posts:
    3
    I guess it was happening because of Raycast which I was using for searching for blocks. I fixed this by separating FindBlockToMove() method from the method MoveToChosenBlock() .