- Home /
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.
Answer by AngryOldMan · Apr 03, 2011 at 02:30 PM
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 $$anonymous$$eith $$anonymous$$ answered I chose the middle ground (as $$anonymous$$eiths answer makes it move nonstop regardless of input so why don't you say anything about that?)
Why? "I cannot for the life of me firgue out what you are trying to do.", then you started pulling out the FAQ. :) Don't take it as bashing, I am just concerned you are trying to put too heavy constraints on people. (Recently you lectured someone in proper English, but the transition from writing poor English to clean English can't be expected to happen over night). Everyone has to start somewhere, both in English and Scripting. If you find the question unclear, there's no need to draw similarities with idiots. I think it's unrespectful - but I agree I put my words no less proper. Apologies.
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 :)
I understand that your remark was with the best intention to lessen my anger. And indeed a grasp of the english language does not come over night, but on the assumption that the people who use Unity have some basic understanding of English why must this understanding be omitted when using this forum. I refere to the FAQ alot as when we first used this site and asked ambiguous questions we were directed to the FAQ to understand the correct etiquette when using this site. It's impossible to find a decent answer to an uncommon question on this site because of people not using it in the way it was
Answer by Statement · Apr 03, 2011 at 02:51 PM
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.Get$$anonymous$$eyDown to Input.Get$$anonymous$$ey. It's a lot more comfortable to play with such controls imo.
Answer by KeithK · Apr 03, 2011 at 01:43 PM
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.
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Drawing objects 1 Answer
(JAVASCRIPT)I have a amnesia style note script, and I need help displaying the words 0 Answers
How to use getkeydown like a DmC4 1 Answer
how can i use getkey and getkeydown for two diffrent functions. 2 Answers