need help badly
im practicing unity can anyone show me example c# script of this. i want when i press space 1 time the object will go all the way up and while the object is going up and i press the space again the object will go all the way down.
hope some1 can help. thank you in advance
Answer by Ady_M · Jul 15, 2017 at 03:01 PM
Ask smaller and more precise questions.
Try to solve one small part at a time.
Also, I would recommend that you try to find a better title for your topic/question, like "Moving an object over time from one position to another"
Topics in your question:
Moving objects every frame:
Reacting to player input:
States (is it on its way up or down):
Using IF-statements and variables (YouTube video) - I did not spend much time looking for the perfect tutorial on using IF-statments for beginners.
hi thank you for your reply and im sorry im totally newbie to unity or game development but i want to learn. anyway ill try to check and study the topics you have suggested and see if it can be use to something i want..
what i really wanted is to make an object move all the way up or down with single keycode until there is collisions. its basically to avoid collisions.
again thank you.
Hmm, let me see if I understand you correctly. All movement is done instantly. Pressing the key should try to instantly move the object to its destination, not over time. But if the object hits something half way up then the object should stop there. Is that correct?
no sir, what i meant is, pressing the key (example "space") the object should fly up and when you press the key again (the space) it should fly down. like press space 1 time to move the object to the left and will not stop until you press the space again to go in the opposite direction..
Answer by Shifty-Geezer · Jul 15, 2017 at 04:14 PM
Sounds like a Flappy Bird zig-zag to me. You should create a vector for movement, and change it every time you press a button. Something like:
Vector3 direction = new Vector3(0,1,0);
float speed = 0.01f;
void Update(){
if (Input.GetKeyDown(KeyCode.Space)) {
direction.y *= -1;
}
transform.position += direction*speed;
}
This checks the space key and, if down, inverts the direction. Every update it moves the object by 'speed' distance. This doesn't factor in screen refresh.
To be honest though, you really need to work through the tutorials to learn the basics of Unity. This is something that you should be able to work out for yourself from the documentation and online tutorials, because creating a game gets infinitely more complex than this! You'll have to spend and effort learning the basics up to moderate ability as otherwise you'll be asking questions like this all the time! ;)
hi sir thank you for your answer and i will try those. and yes i am studying through tutorials but i was just curious about this 1 because i haven't seen 1 yet. so usually the so called " jump" is going up yeah? and usually whats most used for the object to go down gravity fall. is it possible to give an object a jump but going down??
Your answer
Follow this Question
Related Questions
Using "AddForce" with a Character Controller 1 Answer
How do I create Sling-Shot like propulsion? 1 Answer
How to speed up an object with AddForce? 1 Answer
How to create smooth jump with rigidbody.addforce? 2 Answers
How control Rigidbody2d using GetAxis with AddForce and angularVelocity (2D) 1 Answer