Hey guys,
I tried to make a movement script similiar to the game Legend of Grimmrock, but failed miserably because of my minimalistic Java knowledge.
The script i wrote only teleports the player a certain distance, but i need it to move smoothly a certain distance. ( If I hit the “W” key the player moves 1 meter forward and stops).
That is the “script” (i know it sucks) :
var speed = 10;
var numberOfSteps = 10;
function FixedUpdate()
{
if (Input.GetKeyDown("w"))
{
transform.Translate(Vector3.forward * numberOfSteps * speed * Time.deltaTime);
}
if (Input.GetKeyDown("s"))
{
transform.Translate(Vector3.forward * numberOfSteps * speed * (-1) * Time.deltaTime);
}
if (Input.GetKeyDown("a"))
{
transform.Translate(Vector3.left * numberOfSteps * speed * Time.deltaTime);
}
if (Input.GetKeyDown("d"))
{
transform.Translate(Vector3.right * numberOfSteps * speed * Time.deltaTime);
}
}
3 Answers
3
This is not so simple as above - look, that you’re moving player only if key is being pressed. After releasing it, player stops. My proposal is to store distance to go in auxiliary variable and reduce it each time FixedUpdate() is being called, e.g.:
int steps_remaining = 0;
void FixedUpdate()
{
if (Input.GetKeyDown("w"))
{
steps_remaining = 100;
}
if (steps_remaining > 0)
{
transform.Translate(Vector3.forward * numberOfSteps * speed * Time.deltaTime);
steps_remaining--;
}
}
Do the same for each direction.
You can lerp from one spot to another, such as with this.
The mover is either waiting for a key, moving in a direction (and ignoring keys,) or “resting” after a move. If it’s moving in a direction, maybe pressing the opposite key should cancel and move back where it came from (but better to add that after regular moving works.)
The programming trick is make “ready, moving, resting” into a state variable, and key off of that to see what you are doing. Such as:
int action=0; // 0=ready to move, 1=moving to target, 2=resting
Vector3 targ; // where we are moving to. Only if action=1
Vector3 oldPos; // where we go back for cancel
float restTime; // we are resting after move until now. Only if action=2
if(action==0) { // waiting for move
if(Input.GetKey("a")) {
targ = transform.position + Vector3.left*steps;
oldPos = transform.position;
action=1; // tell computer we are moving
}
....
}
else if(action==1) { // moving
transform.position = Vector3.MoveTowards(transform.position, targ, speed);
if(transform.position == targ) {
action=2; // we got there, rest a bit
restTime = Time.time + 0.3f;
}
// check for opposite key being pressed?...
}
else { // action=2, resting
// ignore all until we are done resting
if(Time.time>=restTime)
action=0; // ready for new move
}
I like MoveTowards for a nice smooth move, but Lerp, or the countdown idea above also work. The main key is that the program 1st checks action. If you are moving, it knows to ignore move keys, and so on.
How is it supposed to look? Each key press should "move smoothly a certain distance" Like moving on a board? What happens if a key is pressed while moving (and does just started or almost done matter?)
– Owen-Reynoldslike this : [link text][1] [1]: http://www.youtube.com/watch?v=RKvMDNsEc14 The Object should move as if i keep holding the key down (lets say 1 step, by pushing the button once. if you keep pressing the key the player moves 1 step, stops, then moves 1 step again and so on.
– SolidHerosorry if my grammer is poor, i am from germany
– SolidHero