Search Unity

Controlling a falling character in 2d space

Discussion in '2D' started by SStru1987, Jan 20, 2014.

  1. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    For my prototype, I want to create a character that falls in 2d space and can only be controlled by "sliding" along the x and y axis. Is there a playmaker way or a script that will help me make this happen? Please let me know
     
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
  3. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    thanks a lot for the reference, I'll have more ideas
     
  4. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    its a 2d game. how can i control the character by sliding him from side to side
     
  5. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    think of it as a top down shooter with gravity except the player is falling downward. its a 2d game basically. how can i constrain the character to x and y axis to help him avoid collisions
     
  6. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    The fact that you setup a 2D project in Unity and activate the 2D perspective means that you are automatically constrained to the X and Y axis. When you AddForce to a rigidBody2D, you only have the option to add the force on the X and/or Y axis. Once again you are automatically constrained to the X and Y axis.

    With that said, there are a couple ways to trigger the call to AddForce...
    1) Using onscreen touch events
    2) Or using accelerometer events
     
  7. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    I have my character falling, how do get him to move left and right? I saw the AddForce and was wondering how to implement this. It say's I can't add a character controller since the rigid body is added and collider is on
     
  8. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Do you want to use the accelerometer?

    Or do you want to use on screen controls?

    Or do you want to have the user touch anywhere on the left side of the screen to go left, and anywhere on the right side of the screen to go right?
     
  9. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    For the prototype, i wanted to map slide left with the F key and slide right J key. I will implement the touchscreen controls later. For the Keyboard keys, I want to recreate the sense of the player in front of an arcade machine
     
  10. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    void FixedUpdate() {
    if (Input.GetKeyDown(KeyCode.F)){
    rigidbody2D.AddForce(new Vector2(-100f*Time.deltaTime,0f));
    }else if (Input.GetKeyDown(KeyCode.J)){
    rigidbody2D.AddForce(new Vector2(100f*Time.deltaTime,0f));
    }
    }
     
    Last edited: Jan 21, 2014
  11. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    I created a new script, what are the following steps?
     
  12. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Attach the script to the GameObject that contains the RigidBody2D of your character.
     
  13. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    where do i paste the code in Mono Develop, after void start? and after that how is it implemented?
     
  14. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Sure you can put it after void Start.
    Once you attach it to the GameObject, it automatically works.
     
    Last edited: Jan 21, 2014
  15. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    Do I create this script with Javascript or C#? I'm still having difficulty
     
  16. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
  17. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    No luck. its not working or updating in Unity after its pasted
     
  18. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Ok I updated the code in my earlier post. Try it now ;)
     
  19. SStru1987

    SStru1987

    Joined:
    Jan 3, 2014
    Posts:
    19
    I'll give it a shot. Would you mind helping me out more? I have an ambition of putting it on kickstarter for some additional funding :)…This is my first attempt at making a game and I really believe in this concept and I have other mechanics too. I plan on making a demo first and using it to get feedback to start building the final game