Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Character Movement Help

Discussion in 'Scripting' started by GS.Saturos, Jul 3, 2014.

  1. GS.Saturos

    GS.Saturos

    Joined:
    Jul 2, 2014
    Posts:
    17
    I require help on the following script I currently have for character first person movement.
    The script I have currently allows me to move with the WASD keys freely in all directions and allows me to sprint.
    The problem I have with this script is that I can't seem to figure out a way to implement gravity into it, thus, when I jump, my character jumps up infinitely and can keep jumping when I tap space repeatedly. Jumping also doesn't work unless I move my character with WASD first, which is not what I wanted--I wanted my character to be able to jump when it's in a static position.

    Short version of what I need:
    - Gravity implemented because I can't figure out a way to implement gravitational force in the script.
    - Ability for character to jump only when on ground, and not in air.
    - Ability for character to jump when not moving, because currently my character can only jump when I move with WASD first.

    http://i.imgur.com/0833Fjb.png
     
  2. ande04b

    ande04b

    Joined:
    Aug 29, 2012
    Posts:
    119
    I would try something like implementing a method called Jump(), and then call it if the player is grounded, and presses the space bar. In this method, you would add the upward force of the jump, and at all times, when the player is not grounded, you 'drag' him down by applying some force in a downward direction.

    Don't know if that helps. :)

    EDIT: Why are you multiplying your Vector3.up with the Horizontal Axis?
     
  3. GS.Saturos

    GS.Saturos

    Joined:
    Jul 2, 2014
    Posts:
    17
    Alright, thanks, I'll give that a go, and I multiplied the Vector3.up with vertical axis because I thought jump wouldn't work unless an axis was given.
     
  4. ande04b

    ande04b

    Joined:
    Aug 29, 2012
    Posts:
    119
    Well, the Input.GetAxis("Vertical") basically just means if the player is pressing the W or S key. I don't see why it is relevant in your jump method ;)
     
    GS.Saturos likes this.
  5. GS.Saturos

    GS.Saturos

    Joined:
    Jul 2, 2014
    Posts:
    17
    Ah, thank you! That fixed one of the problems--sorry, I'm new to Unity XD
     
  6. ande04b

    ande04b

    Joined:
    Aug 29, 2012
    Posts:
    119
    No problem, we all need to start somewhere. ;)
    Any more I can help you with?
     
  7. GS.Saturos

    GS.Saturos

    Joined:
    Jul 2, 2014
    Posts:
    17
    That's all for now. Thanks again!