Search Unity

Horribly Complicated Script

Discussion in 'Scripting' started by nickavv, Oct 29, 2007.

  1. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Hey guys. I made a script for controlling my character in an overhead sort of game where the camera's rotation is fixed. It does what I want, but I'm sure its far longer than it has to be. Can you guys figure out how to simplify it at all? Thanks!

    Code (csharp):
    1. var movespeed = 0.1;
    2.  
    3. function Update () {
    4.     Walking();
    5. }
    6.  
    7. function Walking () {
    8.     if (Input.GetButton("Up")) {
    9.         animation.Play ("walk");
    10.         transform.position.z += movespeed;
    11.         transform.eulerAngles.y = 0;
    12.     }
    13.     if (Input.GetButton("Down")) {
    14.         animation.Play ("walk");
    15.         transform.position.z -= movespeed;
    16.         transform.eulerAngles.y = 180;
    17.     }
    18.     if (Input.GetButton("Left")) {
    19.         animation.Play ("walk");
    20.         transform.position.x -= movespeed;
    21.         transform.eulerAngles.y = 270;
    22.     }
    23.     if (Input.GetButton("Right")) {
    24.         animation.Play ("walk");
    25.         transform.position.x += movespeed;
    26.         transform.eulerAngles.y = 90;
    27.     }
    28.     if (Input.GetButton("Up")  Input.GetButton("Left")) {
    29.         animation.Play ("walk");
    30.         transform.eulerAngles.y = 315;
    31.     }
    32.     if (Input.GetButton("Up")  Input.GetButton("Right")) {
    33.         animation.Play ("walk");
    34.         transform.eulerAngles.y = 45;
    35.     }
    36.     if (Input.GetButton("Down")  Input.GetButton("Left")) {
    37.         animation.Play ("walk");
    38.         transform.eulerAngles.y = 225;
    39.     }
    40.     if (Input.GetButton("Down")  Input.GetButton("Right")) {
    41.         animation.Play ("walk");
    42.         transform.eulerAngles.y = 135;
    43.     }
    44.     if (!Input.GetButton("Down")  !Input.GetButton("Up")  !Input.GetButton("Left")  ! Input.GetButton("Right")) {
    45.         animation.Play ("idle");
    46.     }
    47. }
     
  2. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Won't write it for you ;-) However, I'd do it something like this:
    Create a motion vector variable. Modify this based on the input axis (and possibly ammount, if you don't want the movement to be digital). Have the character reorient itself based on the vector direction (and figure out whether it should move backwards or forwards) and then set the apropriate animation.

    I do this for a number of different objects, although not humanoid/walking ones. But it works well and should work with animations without a problem. Give it a whirl. ;)
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I think what you're mainly looking for is Input.GetAxis.

    Usually what's done in this case is something like

    transform.position += Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

    You can then compare the old position with the new one and determine what direction to face, and what animation to play. Should be <10 lines of code.
     
  4. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    I used to have it like that, StarManta, but there was no way (I could think of at least), to make him face the correct direction. Comparing the positions though.. that sounds good. But wouldn't it still be a huge series of If/Thens?
     
  5. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Facing the correct direction based on a vector is pretty simple. In its crudest form you just rotate the model to the corresponding angle. A bit of simple math allows you to make the process much smoother. Then detect which way it is turning (clockwise/counterclockwise, bla bla) and trigger the proper animation. Shouldn't be a list of ifs, maybe a few or a case statement.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Use this:
    Code (csharp):
    1.  
    2. lastPos = transform.position;
    3. //update the position
    4. transform.rotation = Quaternion.LookRotation(transform.position - lastPos, Vector3.up);
    5.  
    Vector3 and Quaternion are fantastic class with a lot of incredible utilities built in - your games will benefit greatly from learning how to use them.