Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2.5 jumping and moving

Discussion in 'Scripting' started by Ded_Hed_Hyde, Apr 20, 2014.

  1. Ded_Hed_Hyde

    Ded_Hed_Hyde

    Joined:
    Mar 10, 2014
    Posts:
    22
    Hello all. Been tinkering with a 2.5 platform game. Learning really. I finally got my movement script done for my player and was wondering if I could get someone to crit it for me? Is there better ways to do it? Is it effiecient? Just things like that. I've scoured the web and this site and read up as much as i could. Thanks in advance.

    Code (csharp):
    1.  
    2. //Player control script
    3. //Inspector variables
    4. var speed                     : int               = 6.0;            // speed we move
    5. var jumpSpeed                 : int               = 8.0;            // how fast we jump
    6. var gravity                   : int               = 20.0;           // force of gravity
    7. var facingRight               : boolean           = true;
    8. var distToGround              : float             = 3.0;
    9.  
    10. //Private variables
    11. private var moveDirection     = Vector3.zero;                       // player move direction
    12. private var vertVel           : float             = 0;              // vertical velocity
    13. private var isGrounded        : boolean           = true;           // bool to check if we are grounded
    14.  
    15. function Update ()
    16. {
    17.     if (Input.GetKeyDown (KeyCode.A))
    18.     {
    19.         transform.right = new Vector3(0, 0, 1);
    20.         facingRight = false;
    21.     }
    22.     else if (Input.GetKeyDown (KeyCode.D))
    23.     {
    24.         transform.right = new Vector3(0, 0, -1);
    25.         facingRight = true;
    26.     }
    27.    
    28.     var translate : float = Input.GetAxis("Vertical");
    29.  
    30.     if(facingRight == false  Input.GetKey(KeyCode.A))
    31.     {
    32.         transform.Translate(Vector3(0,0,5 * Time.deltaTime));
    33.     }
    34.     if (facingRight == true  Input.GetKey(KeyCode.D))
    35.     {
    36.         transform.Translate (Vector3(0,0,5 * Time.deltaTime));
    37.     }
    38.    
    39.     groundCheck = (Physics.Raycast(transform.position, -Vector3.up, distToGround));
    40.     if (Input.GetButtonDown ("Jump")  groundCheck == true)
    41.     {
    42.         rigidbody.AddRelativeForce(0, jumpSpeed, 0);
    43.     }
    44.  
    45. }
    46.