Search Unity

[SOLVED-KIND OF] how come this is not making me jump?

Discussion in 'Scripting' started by smokingbunny, Nov 28, 2014.

  1. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    hello all,

    well i think im set on using unity with the free edition until i get the cash to get the full version and have been running through a lynda tutorial for 2d game dev. ive done it before, but now that im set on using unity im running through it again.

    but one part im having trouble with is pretty much near the beginning, though i have my assets loaded and having different animations, boom thats done. but the jumping/rocket controls just are not working for me. and im more than sure this worked when i went through it before, about 5 months ago.
    but i cant see a problem, nor am i getting any errors.

    though i can find a jump control here easily, id just like to know why this is not working. im baffled to say the least

    here is my code. 2 files

    thanks

    player.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class player : MonoBehaviour {
    6.  
    7.     public float speed = 10f;
    8.     public Vector2 maxVelocity = new Vector2(3,5);
    9.     public bool standing;
    10.     public float jump = 15f;
    11.     public float airSpeed = 0.3f;
    12.  
    13.     private playerController controller;
    14.  
    15.     void Start(){
    16.         controller = GetComponent<playerController>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.         var forceX = 0f;
    22.         var forceY = 0f;
    23.  
    24.         var absVelX = Mathf.Abs (rigidbody2D.velocity.x);
    25.         var absVelY = Mathf.Abs (rigidbody2D.velocity.y);
    26.  
    27.         if(absVelY < 0.2f){
    28.             standing = true;
    29.         } else {
    30.             standing = false;
    31.         }
    32.  
    33.         if(controller.moving.x != 0){
    34.             if(absVelX < maxVelocity.x){
    35.                 forceX = standing ? speed * controller.moving.x : (speed * controller.moving.x * airSpeed);
    36.                 transform.localScale = new Vector3(forceX > 0 ? 1 : -1,1,1);
    37.             }
    38.         }
    39.         if(controller.moving.y > 0){
    40.             if(absVelY < maxVelocity.y)
    41.                 forceY = jump * controller.moving.y;
    42.         }
    43.         rigidbody2D.AddForce (new Vector2 (forceX, forceY));
    44.     }
    45. }
    46.  
    playerController.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class playerController : MonoBehaviour {
    6.  
    7.     public Vector2 moving = new Vector2();
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.         moving.x = moving.y = 0;
    17.  
    18.         if(Input.GetKey("left")){
    19.             moving.x = -1;
    20.         } else if(Input.GetKey("right")){
    21.             moving.x = 1;
    22.         }
    23.  
    24.         if(Input.GetKey("up")){
    25.             moving.y = 1;
    26.         } else if(Input.GetKey("down")){
    27.             moving.y = -1;
    28.         }
    29.     }
    30. }
    31.  
    32.  
     
  2. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    well i kept trying other things with this code but could not find why it was not working. so ive done the next best thing and have started to use the unity tutorials and found something good which was a 2d character controller

    great