moving a character a fixed amount help please

ive been trying a while now but cant seem to get this to work, i need my player to move to the right a fixed amount at a fixed speed but it only moves a very small amount...can someone please just fix my code or explain to me what the problem is.

var tap: boolean = false;
var timer: float = 0.0;
var player: Transform;

function Update()
{
    timer += Time.deltaTime;
    if(Input.GetKeyDown(KeyCode.RightArrow))
    {
        tap = true;
        if(tap)
        {
            timer = 0;
        }
        if(timer < 1.0)
        {
            player.position.x +=5*Time.deltaTime;
        }
        else
        {
           tap = false;
        }
    }
}

Use code tags to show code. In your edit window highlight your code and click the "" icon at the top, it'll wrap it up for you and keep the formatting.

Whoops, correction, it's the binary icon. Not the quotes.

3 Answers

3

The way to move your character in a direction at a fixed speed, is to translate it by a velocity.

var speed = 5;

function Update()
{
    transform.Translate(Vector3.right * speed * Time.deltaTime);
}

This will move your object 5 units per second to the right.

I cannot for the life of me firgue out what you are trying to do. Some disambiguation in your question would be a huge help, from looking at your code (which I had to format myself because you can't be bothered) I've no clue what effect your trying to achieve. What is all this about tap and timer? It appears like when you press a button you want the timer to reset to 0 but you also have it that the timer keeps adding time.deltatime to itself? Do you get an error when compiling this code? Errors during runtime? Basically what I'm saying is your question is too vague to give a definitive answer.I can tell you for sure that if your trying to move a player this is definatly the wrong way to go about it.

The problem with your question is you have specified that you want it to move at a fixed speed and a fixed amount but not the time scale that you want it to be in, eg, Speed = distance/ time. distance = speed * time, time = distance/speed. So you want it do you want it to move 5 units at 5 units/second?or 5 units at 1 unit/second? etc

Anyway less about basic physics and how poor your question is and back to an definitive answer. if you are using a character controller and what it to move when you hold down an arrow key and stop moving when you let go use something with this effect

var speed : float = 1;
moveDirection = Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0);
moveDirection = transform.TransformDirection(moveDirection*speed);

if you want your character to move a fixed amount then stop no matter how long you hold down the button then use something to this effect

var DistanceAmount : float = 1;
var KeyHasBeenPressed : boolean = false;

if (Input.GetKeyDown(KeyCode.RightArrow))
{
    if (KeyHasBeenPressed == false)
    {
        transform.position += DistanceAmount;
        KeyHasBeenPressed = true:
    }
}

if (Input.GetKeyUp(KeyCode.RightArrow))
{
    KeyHasBeenPressed = false;
}

what keith has said will move your object to the right non-stop at said velocity (5 units/second) You can incorporate that into a key press to get some effect that you may or may not want, depending on what you actually want because you havn't specified properly.

in the FAQ for this site which you havn't read (or havn't understood) it clearly states

What kind of questions can I ask here?

Unity questions, of course! As long as your question is:

detailed and specific

written clearly and simply

of interest to at least one other Unity user somewhere

... it is welcome here. No question is too trivial or too "newbie". Oh yes, and it should be about Unity.

Please look around to see if your question has already been asked (and

maybe even answered!) before you ask.

This website is already has too many narrow-minded and self-absorbed idiots who's questions don't get answerewd so they post duplicates until someone writes them a piece of code that works how they want. Learn to code properly and don't come here everytime you cant figure out the answer look other places, google it, scroll through the archives of related questions here (its what the site is primarily for!!!) use the script reference site provided by unity, use a tutorial that will walk you through it step by step. Like Unity says there is no such question that is too "newbie" but there is a load of stupid questions here that could have been answered had the person gone to look for the answer themselves instead of crying like b|<% and posting question after question here. Go learn independantly and when you get stuck then you post a question here, or if you need to figure out a way to do something and you don't just want someone to hold your hand and write your code.

I thought it was pretty clear. He want to make movement sort of like old skool games like boulder dash. He want to move 5 units every second, but not continously.

Of course your going to understand it, you're a scripting genius. Fair play but it wasn't clear to me, nor from looking back at his (now formatted) code does it become clear. His question doesn't specify that he wishes to move a certain amount of units but not continously, and how harde was it for you to type that? Why couldn't he have said that, cleared the whole problem right up. And from what Keith K answered I chose the middle ground (as Keiths answer makes it move nonstop regardless of input so why don't you say anything about that?)

Don't get hung up on my silly remark, I put it wrong. You're doing a good job here and you've placed many good answers. But I think we all could learn to live with a few grammar mistakes and make best assumptions of what the authors intentions were :)

intended to be used. I wish to be more helpful and checking this site often is a good way to keep my unity knowledge up when not working on developing a project directly. My point is a little common knowledge and etiquette goes a long way, if everyone just called emergancy services every time the power goes out and they don't know where the breaker is then the country would be screwed. Common knowledge to look for the breaker yourself, then etiquette to use the correct channels of search to return an answer (like calling the landlord 1st) lol

And I know that I cross ways with many regulars here who don't think I should be paying any cent until the question is crystal clear. But I find it valuable to learn to try understand what is meant with minimal information. In several work places it's been the same - the clients/designers don't know how to put stuff into words that are easy for a programmer to learn so that's why I try to brush up on problem intuition :) It's great fun trying to solve with what little info is provided.

If you are looking for "blocky" movement (like grid movement), here's something you could do. I make use of CoUpdate to block input code while character is sliding to the next position. I tested it and it works on my machine.

var player : Transform;
var speed : float = 5;

while(true) yield CoUpdate();

function CoUpdate() {
    if (Input.GetKeyDown(KeyCode.RightArrow))
        yield Move(Vector3.right);
    else if (Input.GetKeyDown(KeyCode.LeftArrow))
        yield Move(Vector3.left);
    else if (Input.GetKeyDown(KeyCode.UpArrow))
        yield Move(Vector3.forward);
    else if (Input.GetKeyDown(KeyCode.DownArrow))
        yield Move(Vector3.back);
    else 
        yield;
}

function Move(distance : Vector3) {
    var pt = player.transform;
    var goal = pt.position + distance;
    while (pt.position != goal) {
        pt.position = Vector3.MoveTowards(pt.position, goal, speed * Time.deltaTime);
        yield;
    }
}

If you want continuous movement you can change all the Input.GetKeyDown to Input.GetKey. It's a lot more comfortable to play with such controls imo.