• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[UnityScript Question] Making movement smooth

Status
Not open for further replies.
Level 1
Joined
Dec 7, 2008
Messages
823
[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.
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.
#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:
Level 1
Joined
Dec 7, 2008
Messages
823
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?
 
Status
Not open for further replies.
Top