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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Precise movement

Discussion in '2D' started by Racm_Games, Nov 25, 2015.

  1. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    Hi,

    Well guys I am having trouble with a game that I am currently making, is in pretty early stages but I am trying to get the basics done in order to move on. My problem is the following, I am trying to make the character move to certain positions of the game when the user presses certain button, kinda of like, moving in a grid, well that was easy no problem with that, but I notice that the change of position is too fast!! I have tried different ways of moving the character but none make the character land in that exact position I desire.

    Code (CSharp):
    1.  
    2. if (Input.GetKeyDown (KeyCode.D) && !inhabilitado && center)
    3.             {
    4.             hero.transform.position = new Vector2(plat03.transform.position.x , hero.position.y);
    5.             }
    There is an example of the code I have right now...

    Basically, I want the player to move from point A to B in a fluid way(landing in the exact point I want) so that when I insert animations it will look good.

    NOTE: I have tried MovePosition and others similar ways but the player doesn't land where I want tho the movement is very fluid
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use Lerp.

    --Eric
     
  3. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    It seems to give me the same result as the code I posted, the position of the player still changes abruptly. Thanks for the quick reply
     
  4. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    Well, there is a way for doing this but I think is inefficient because it will make me try different combinations of ...AddForce and add a high level of friction to the ground so that when the player collides with it, he won't move a bit. I don't know if this is the only way posible for what I want so any other ideas will be very much appreciated.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  6. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    Oh dude thanks!!, with the script in the wiki article that you created helped me a lot to understand how to use lerp. Thanks again!!!
     
  7. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    Sorry to bother you again, but I am trying to make some changes to the script you created in order to avoid the player to move to the right when a boolean is true, but I can't manage to do it. I was wondering if you can help me out please or someone else that knows about lerps..