Search Unity

Stop velocity at the position you want

Discussion in 'Scripting' started by Comolokko, Jul 24, 2014.

  1. Comolokko

    Comolokko

    Joined:
    Jul 24, 2014
    Posts:
    28
    Hi there!
    I am a noob coder who is interested in game development. Trying to make an infinite runner game but the character is a bird so there isn't gravity. I have searched the forum and on the google, i couldn't find the solution or didn't use the right words to search.

    I have this script on a bird that makes bird always goes to forward.
    Code (CSharp):
    1. void FixedUpdate() {
    2.         rigidbody2D.velocity = new Vector2 (forwardSpeed, rigidbody2D.velocity.y);
    3.     }
    If i use this code transform.position = new Vector2(transform.position.x, 4f); to change the position of bird, it goes instantly there. But i want it to go smoothly like velocity. If i use this rigidbody2D.velocity = new Vector2 (forwardSpeed, forwardSpeed); to move bird up and down, i can't stop the bird at the position that i want.
    Code (CSharp):
    1. int yer = 1;
    2. void Update () {
    3.  
    4.         if (yer == 0 && Input.GetKeyDown (KeyCode.UpArrow))
    5.         {
    6.             Debug.Log ("You are at top");
    7.         }
    8.         else if (yer == 1 && Input.GetKeyDown (KeyCode.UpArrow))
    9.         {
    10.             yer--;
    11.             transform.position = new Vector2(transform.position.x, 4f);    
    12.         }
    13.         else if (yer == 2 && Input.GetKeyDown (KeyCode.UpArrow)) {
    14.             yer--;
    15.             transform.position = new Vector2(transform.position.x, 0f);
    16.         }
    17.         else if (yer == 0 && Input.GetKeyDown (KeyCode.DownArrow)) {
    18.             yer++;
    19.             transform.position = new Vector2(transform.position.x, 0f);
    20.         }
    21.         else if (yer == 1 && Input.GetKeyDown (KeyCode.DownArrow)) {
    22.             yer++;
    23.             transform.position = new Vector2(transform.position.x, -4f);
    24.         }
    25.         else if (yer == 2 && Input.GetKeyDown (KeyCode.DownArrow)) {
    26.             Debug.Log("You are at bot");
    27.         }
    28.     }
    Thanks for your help!