1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The Lich King demands your service! We've reached the 19th edition of the Icon Contest. Come along and make some chilling servants for the one true king.
    Dismiss Notice
  4. The 4th SFX Contest has started. Be sure to participate and have a fun factor in it.
    Dismiss Notice
  5. The poll for the 21st Terraining Contest is LIVE. Be sure to check out the entries and vote for one.
    Dismiss Notice
  6. The results are out! Check them out.
    Dismiss Notice
  7. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  8. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  9. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[UnityScript Question] Making movement smooth

Discussion in 'Unity 3D' started by Zelda.Alex, Oct 21, 2012.

  1. Zelda.Alex

    Zelda.Alex

    Joined:
    Dec 7, 2008
    Messages:
    778
    Resources:
    0
    Resources:
    0
    [SOLUTION FOUND]Someone close this thread as I found the answer on the unity forum link added at the end of the post.
    ----------------
    For my snakes and ladders game I have 2 players. The logic for moving them is working properly but the problem is that they are moving too fast. In many cases it is not possible to follow their movement with my eyes. What can I do?

    If any other explanation is needed for giving me advice then please tell.
    Explanation of the code
    The way I am moving my players is by measuring 2 distances in the start() and using them as constant. The main movement is done in the update() function. The switch statement is for changing positions in case of a snake or a ladder.
    Code Used for moving
    #pragma strict

    //These are set from GUI
    var p1 : GameObject;
    var p2 : GameObject;
    var cube : GameObject;
    var cubeUp : GameObject;
    var cubeRight : GameObject;

    //These remain Constant
    private var moveRight : Vector3;
    private var moveLeft : Vector3;
    private var moveUp : Vector3;
    private var moveDown : Vector3;

    //These change according to player's position
    private var p1_pos : int = 1;
    private var p2_pos : int = 1;

    //This decides whose turn
    private var turn : int = 0;
    //This is for current moves
    private var moves : float = 0.0;

    //Functions to be called
    function move(Left : float, Right : float, Up : float, Down : float)
    { //L R U D
    //Automatically decides which player to move according to turn
    if(turn ==0)
    p1.transform.Translate((moveRight * Right)+(moveLeft * Left) + (moveUp * Up) + (moveDown * Down));
    else
    p2.transform.Translate((moveRight * Right)+(moveLeft * Left) + (moveUp * Up) + (moveDown * Down));
    }
    function in_range(lower : int, higher : int): boolean
    {//Checks whether 2 numbers in a range
    var pos : int;

    if(turn ==0)
    pos = p1_pos;
    else
    pos = p2_pos;

    if( pos >= lower && pos <= higher)
    return true;
    else
    return false;
    }
    function on_snake_or_ladder(pos : int) : int
    {
    switch(pos)
    {
    //Ladders L R U D
    case 2:
    move(0,1,1,0); return(18);
    case 11:
    move(0,0,2,0); return(31);
    case 12:
    move(1,0,1,0); return(28);
    case 22:
    move(1,0,1,0); return(40);
    case 36:
    move(3,0,3,0); return(62);
    case 41:
    move(0,1,1,0); return(59);
    case 46:
    move(0,0,1,0); return(55);
    case 70:
    move(3,0,3,0); return(94);
    case 77:
    move(0,0,1,0); return(84);
    case 85:
    move(1,0,1,0); return(97);

    //Snakes L R U D
    case 21:
    move(0,5,0,1); return(15);
    case 23:
    move(0,3,0,2); return(6);
    case 29:
    move(3,0,0,1); return(15);
    case 35:
    move(3,0,0,2); return(18);
    case 47:
    move(0,2,0,1); return(32);
    case 52:
    move(6,0,0,2); return(38);
    case 71:
    move(3,0,0,4); return(34);
    case 82:
    move(0,0,0,3); return(59);
    case 95:
    move(3,0,0,2); return(78);
    case 99:
    move(0,0,0,2); return(79);
    default:
    break;
    }
    return(-1);
    }
    //Functions to be called END HERE

    function Start()
    {
    var distance : Vector3 = p2.transform.position - p1.transform.position;

    moveRight = (cubeRight.transform.position - cube.transform.position);
    moveLeft = - moveRight ;
    moveUp = (cubeUp.transform.position - cube.transform.position);
    moveDown = - moveUp;

    p1.transform.Translate(cube.transform.position - p1.transform.position);
    p2.transform.position = p1.transform.position;
    p2.transform.Translate(distance);
    }

    function OnGUI ()
    {
    if(moves == 0.0)
    {
    if(GUI.Button(Rect (20,70,80,40), "Throw\n Dice"))
    {
    var moves_int : int;
    moves_int = Random.Range(1,6);
    moves = moves_int;
    }
    }
    }

    function Update()
    {
    if(moves > 0) //Executes if any player is yet to move
    {
    if(p1_pos < 100 && turn == 0)
    {
    if(in_range(1,9) || in_range(21,29) || in_range(41,49) || in_range(61,69) || in_range(81,89))
    move(0,1,0,0);
    else if(in_range(11,19) || in_range(31,39) || in_range(51,59) || in_range(71,79) || in_range(91,99))
    move(1,0,0,0);
    else if((p1_pos % 10 == 0)&&(p1_pos != 100))
    move(0,0,1,0);

    p1_pos = p1_pos + 1;
    }
    else if(p2_pos < 100 && turn == 1)
    {
    if(in_range(1,9) || in_range(21,29) || in_range(41,49) || in_range(61,69) || in_range(81,89))
    move(0,1,0,0);
    else if(in_range(11,19) || in_range(31,39) || in_range(51,59) || in_range(71,79) || in_range(91,99))
    move(1,0,0,0);
    else if((p2_pos % 10 == 0)&&(p2_pos != 100))
    move(0,0,1,0);

    p2_pos = p2_pos + 1;
    }
    moves = moves - 1;

    if(moves == 0) //If the player is done moving
    {
    //This executes when player has done moving his turn.
    //After that snake bites or ladder is climbed
    var returned_pos : int;

    if(turn == 0)
    {
    returned_pos = on_snake_or_ladder(p1_pos);
    if(returned_pos != -1)
    {
    p1_pos = returned_pos;
    }
    }
    else
    {
    returned_pos = on_snake_or_ladder(p2_pos);
    if(returned_pos != -1)
    {
    p2_pos = returned_pos;
    }
    }

    turn = (turn + 1) % 2;
    }//If the Player is done moving ENDS here
    }
    }//Update() function ENDS here

    Unity Forum Discussion of the same
     
    Last edited: Oct 21, 2012
  2. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,336
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
    Or you could just change the movement to 1 unit and cut it in pieces, moving like 0.5 every second (using Delta.Time)
     
  3. Zelda.Alex

    Zelda.Alex

    Joined:
    Dec 7, 2008
    Messages:
    778
    Resources:
    0
    Resources:
    0
    I don't understand that thing about Delta.Time that everyone keeps on saying. The Update() function woks every frame then how can we decide time by the delta.time? Any example?
     
  4. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,336
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
    Code (Text):

    var x:int=0;
    Function Update(){
    x=x+Delta.Time;
    }
     
    This will give you the time in ms since the time the script is loaded.