Search Unity

Making an object move

Discussion in 'Scripting' started by carking1996, Feb 11, 2011.

Thread Status:
Not open for further replies.
  1. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Hello,

    How do I make an object move using the Up arrow? I'm using this code atm:

    function Update () {
    Input.GetKeyDown("up");
    transform.Translate(.001, 0, 0);
    }

    But, when I go to play, it moves without me pressing it:confused: .And I don't want the object to move in only 1 direction, because it is the player. Any help is appreciated. ;)
     
  2. defjr

    defjr

    Joined:
    Apr 27, 2009
    Posts:
    436
    Try this:
    Code (csharp):
    1.  
    2. if (Input.GetKey(KeyCode.UpArrow)) {
    3.      transform.Translate(Vector3.forward * Time.deltaTime);
    4. }
    5.  
     
  3. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Cool! Thanks earl! Another question. What about making it rotate when pressing the arrow keys? :)

    EDIT, nevermind. I got it. ;) Anyways, I'm going to figure out how to make it rotate when you press the right arrow! :D

    EDIT2: How do I make it rotate? This is the script I'm using:

    function Update () {
    if (Input.GetKey(KeyCode.LeftArrow)) {
    transform.Translate(Vector3.left * Time.deltaTime);
    transform.Rotate(Vector3.left * Time.deltaTime);
    }
    }

    Help is appreciated! :D
     
    Last edited: Feb 11, 2011
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Both are pretty simple programatically, but I suggest a mathematical approach:

    Code (csharp):
    1.  
    2.     var movSpeed=3.0;
    3.     var rotSpeed=60.0;
    4.     var mov = Input.GetAxis("Vertical") * Time.deltaTime * movSpeed;
    5.     var rot = Input.GetAxis("Horizontal") * Time.deltaTime * rotSpeed;
    6.     transform.Translate(Vector3(0,0,mov));
    7.     transform.Rotate(Vector3(0,rot,0));
    8.  
    The Unity Script Reference will help you avoid small questions.. ;)

    http://unity3d.com/support/documentation/ScriptReference/Transform.html
     
    Last edited: Feb 11, 2011
  5. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    That one confuses me. Is there an easier way like the one Earl and I showed? I am a beginner, so, I wanna learn simply for now. ;)
     
    aghoedwin1 likes this.
  6. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    About as simple as it can be made for keyboard only inputs. Just setup your keys in the inspector to include Forward, Reverse, Turn Left, Turn Right. Hope it helps

    Code (csharp):
    1. var Speed : float = 5.0;
    2. var RotationSpeed : float = 5.0;
    3.  
    4. function Update () {
    5.  
    6. if(Input.GetButton("Forward")){
    7. transform.Translate(Vector3.forward * Time.deltaTime * Speed, Space.Self);
    8. }
    9.  
    10. if(Input.GetButton("Reverse")){
    11. transform.Translate(Vector3.forward * Time.deltaTime * -Speed, Space.Self);
    12. }
    13.  
    14. if(Input.GetButton("Turn Left")){
    15. transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
    16. }
    17.  
    18. if(Input.GetButton("Turn Right")){
    19. transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
    20. }
    21.  
    22. }
     
  7. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Thanks for that. It works great. But, The object moves to fast. I modified the code to look like this, but it still is too fast. :confused:

    var Speed : float = 1.0;
    var RotationSpeed : float = 5.0;

    function Update () {

    if(Input.GetKey(KeyCode.UpArrow)){
    transform.Translate(Vector3.forward * Time.deltaTime * Speed, Space.Self);
    }

    if(Input.GetKey(KeyCode.DownArrow)){
    transform.Translate(Vector3.forward * Time.deltaTime * -Speed, Space.Self);
    }

    if(Input.GetKey(KeyCode.LeftArrow)){
    transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
    }

    if(Input.GetKey(KeyCode.RightArrow)){
    transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
    }

    }

    Thanks!
     
  8. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    if it's moving to fast for you go ahead and reduce the speed down to:

    var Speed : float = 0.1;

    Aalso check your objects scale it may be very small.
     
  9. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    I figured it out. The variables were in the inspector. ;) Thanks for all your guys help. :) Last question, How do I make the object move at an increasing rate? Instead of 1 speed?
     
  10. eicon11

    eicon11

    Joined:
    Feb 11, 2011
    Posts:
    14
    you could do something like a incresing speed variable

    var CurrentSpeed : float = 0;
    var Speed : float = 1.0;
    var RotationSpeed : float = 5.0;

    function Update () {

    transform.Translate(Vector3.forward * Time.deltaTime * CurrentSpeed , Space.Self);

    if(Input.GetKey(KeyCode.UpArrow)){
    if(CurrentSpeed < Speed )
    CurrentSpeed += Time.deltaTime;
    }

    if(Input.GetKey(KeyCode.DownArrow)){
    if(CurrentSpeed > -Speed )
    CurrentSpeed -= Time.deltaTime;
    }

    if(Input.GetKey(KeyCode.LeftArrow)){
    transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
    }

    if(Input.GetKey(KeyCode.RightArrow)){
    transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
    }

    }
     
  11. eicon11

    eicon11

    Joined:
    Feb 11, 2011
    Posts:
    14
    And then in the next step you can decrease the speed automaticly :)

    var CurrentSpeed : float = 0;
    var Speed : float = 1.0;
    var RotationSpeed : float = 5.0;

    function Update () {

    transform.Translate(Vector3.forward * Time.deltaTime * CurrentSpeed , Space.Self);

    if(CurrentSpeed < 0) {
    CurrentSpeed += 0.25 * Time.deltaTime;
    } else {
    CurrentSpeed -= 0.25 * Time.deltaTime;
    }

    if(Input.GetKey(KeyCode.UpArrow)){
    if(CurrentSpeed < Speed )
    CurrentSpeed += Time.deltaTime;
    }

    if(Input.GetKey(KeyCode.DownArrow)){
    if(CurrentSpeed > -Speed )
    CurrentSpeed -= Time.deltaTime;
    }

    if(Input.GetKey(KeyCode.LeftArrow)){
    transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
    }

    if(Input.GetKey(KeyCode.RightArrow)){
    transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
    }

    }
     
  12. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Thats more complicated.. This is by no means the only way to do this but it's what i've come up with.

    This will only get you to move forward at an accelerated rate you can add the rest.


    Code (csharp):
    1. var MaxSpeed : int = 5;            //how fast do you want to go.
    2. var CurSpeed : float = 0.0;        //DO NOT CHANGE..
    3. var AccelRate : float = 0.008;    //how fast do you want to accelerate
    4. var MaxAccel : float = .03;        //what do you want your max acceleration to be...
    5. var CurAccel : float = 0.0;         //DO NOT CHANGE
    6. var Deccel : float = 0.008;        //how fast do you want to stop
    7.  
    8. function Update () {
    9.  
    10.     if (CurSpeed !=0  !Input.GetButton("Forward")  !Input.GetButton("Reverse") ){
    11.     CurSpeed = Mathf.Lerp(CurSpeed, 0, Time.time * Deccel);
    12.     CurAccel = 0;
    13.     }else{
    14.         if (CurSpeed < MaxSpeed  Input.GetButton("Forward")) {
    15.             if (CurAccel < MaxAccel){
    16.                 CurAccel = Mathf.Lerp(CurAccel, MaxAccel, Time.time * AccelRate);
    17.             }
    18.                 CurSpeed += CurAccel;
    19.         }else{
    20.             if (CurSpeed == MaxSpeed  Input.GetButton("Forward")){
    21.                 CurSpeed = MaxSpeed;
    22.             }
    23.         }
    24.     }
    25.     transform.Translate(Vector3.forward * Time.deltaTime * CurSpeed, Space.Self);
    26. }
    It may look messy in the forum. Thats because of the way it's handling word wrapping and spacing. Just copy it into you javascript and it will look better.
     
    Last edited: Feb 11, 2011
  13. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Thanks. It works. But, when I edit the Speed var, it only goes up to that speed. I want to make the car slower, so I edited that. But, How can I make it go faster than the Speed var?

    EDIT: Just saw the newest reply. Let me read that.

    EDIT2: I get an error: ...'expecting }, found ". :confused: And, can you explain to me what all that code means? It'll help me learn easier. ;)
     
    Last edited: Feb 11, 2011
  14. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    The only way to make it go faster is to increase the Speed var, or make a "Turbo" key that would add too the Speed var.

    this is a non working example.

    var turbo : int = 5;

    if(Input.GetButton("Turbo"){
    Speed = Speed + Turbo;
    }


    Start with something like that. play around and see what you can come up with.
     
  15. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
  16. eicon11

    eicon11

    Joined:
    Feb 11, 2011
    Posts:
    14
    Or you could just remove the Speed variable, but than your object can increase/decrease to any speed.
    But if this is what you want :)

    var CurrentSpeed : float = 0;
    var RotationSpeed : float = 5.0;

    function Update () {

    transform.Translate(Vector3.forward * Time.deltaTime * CurrentSpeed , Space.Self);

    if(CurrentSpeed < 0) {
    CurrentSpeed += 0.25 * Time.deltaTime;
    } else {
    CurrentSpeed -= 0.25 * Time.deltaTime;
    }

    if(Input.GetKey(KeyCode.UpArrow)){
    CurrentSpeed += Time.deltaTime;
    }

    if(Input.GetKey(KeyCode.DownArrow)){
    CurrentSpeed -= Time.deltaTime;
    }

    if(Input.GetKey(KeyCode.LeftArrow)){
    transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
    }

    if(Input.GetKey(KeyCode.RightArrow)){
    transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
    }

    }
     
  17. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    I like that code the best. But, without the Speed var, it goes too fast. :(
     
  18. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Double post. I got it to be slower. :D But, There's 2 things that I dont like. Is there a way to not make it rotate when in a stationary position? And, is there a way to make it brake faster? That's all i need. Thanks :)
     
  19. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I'm assuming you're trying to simulate a car or other similar vehicle?

    If so, a simple (non-physics-based) solution would be to make the rate of rotation proportional to the vehicle's linear speed. (In other words, the faster it's moving, the faster it can turn.)

    If you want to be a little more realistic, you can compute the maximum rotation based on the current linear speed and the vehicle's turn radius.
     
  20. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Yes, i am trying to make a car move. I just don't want to use the car tutorials because it's too complicated for me to use. :)
     
  21. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Carking, I believe that you need to start off a little simpler than a car. There are many tutorials out there, none of them are simple, and no one here is going to offer you a "simple" explanation of one. Mostly because it is not simple.

    I admire your determination though, this and the other post concerning figuring out how the wheels turn and such.

    There are two basic ways to build vehicles in unity. One through script, where you have a box, and let the physics gravity work out how things fall and such, but use the script to move your block around. Second, use physics to control the bulk of the work and simply control how the wheels are powered and turned.

    Again, neither is simple.

    A great way to learn how things work in Unity is to start building simple objects in the explorer and attaching things like rigid bodies and such. Make things fall, push things around. Your goal is to learn 1: how things are put together in Unity, 2: how to do basic controls.
     
  22. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    I know how to use most stuff in unity. :) This method that I am using is working so far. ;)
     
  23. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Double post... Is there any other easier car scripts? This one I am using isn't the best(for a car). But, I don't want to use the tutorial's one. I just want something basic, like this one, but more for a car. ;)
     
  24. Diamonddoggames

    Diamonddoggames

    Joined:
    Nov 8, 2016
    Posts:
    2
    noting! I tried everything still NOT WORKING:mad:
     
  25. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    Don't necro. This thread is nearly 6 years old. If you have a specific problem, please post a new thread and provide details. The content in this thread probably won't be useful due to its age.
     
    Ryiah likes this.
Thread Status:
Not open for further replies.