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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Character Controller - Jump?

Discussion in 'Scripting' started by Metal Head, Aug 14, 2010.

Thread Status:
Not open for further replies.
  1. Metal Head

    Metal Head

    Joined:
    Aug 14, 2010
    Posts:
    411
    Hey guys, sorry for posting a new post so soon, but I have another problem. I'm writing a player script from scratch. So far I made it walk successfully.
    I want to make the player jump, I looked at the sample scripts, searched for functions in the documentation, but no luck.

    I saw this in the FPSWalker script:
    Code (csharp):
    1. if (Input.GetButton ("Jump")) {
    2. moveDirection.y = jumpSpeed;
    3. }
    4.    
    5. moveDirection.y -= gravity * Time.deltaTime;
    it doesn't really make sense to me. Were does this "y" come from and where is moveDirection.y used to make the player jump.


    Here's my code so far, I tried to detect if the player is on the ground, but still no success.

    I know there's a way to apply force on a rigid body, but how would I apply uppward force to a character controller?

    Code (csharp):
    1. var speed = 10.0;
    2. var onground = false;
    3.  
    4. function Update () {
    5. transform.RotateAround (Vector3.up, Input.GetAxis ("Mouse X")/5);
    6. var controller : CharacterController = GetComponent(CharacterController);
    7. var moveDir = new Vector3(Input.GetAxis("Horizontal")*5, 0, Input.GetAxis("Vertical")*5);
    8. controller.SimpleMove(transform.TransformDirection(moveDir));
    9.  
    10.  
    11.  
    12. if(controller.collisionFlags == CollisionFlags.Below)
    13. {
    14. onground = true;
    15. }
    16. else
    17. {
    18. onground = false;
    19. }
    20.  
    21. print(onground);
    22.  
    23.  
    24. }


    EDIT:
    Alright, I made it jump, but I still can't check if I'm touching the ground..
    I used:
    Code (csharp):
    1. transform.Translate(Vector3.up * 40 * Time.deltaTime);
     
  2. vlk2

    vlk2

    Joined:
    Nov 4, 2009
    Posts:
    43
    the moveDirection is a Vector3, used in the Lerpz 3d game tutorial of unity.

    the code is something like this:

    Code (csharp):
    1. var flags = myController.Move((moveDirection) * Time.deltaTime);
    2.     grounded = (flags  CollisionFlags.CollidedBelow) != 0;
    myController is the character controller.

    It is better if you use the .Move for character controller, and use it once/frame

    it can be in update()
     
  3. Metal Head

    Metal Head

    Joined:
    Aug 14, 2010
    Posts:
    411
    Oh,I know why the game doesn't know when the player is on the ground and when he's not. The collisionflags is only returned by the charactercontroller.move function, but I'm not using it. How would I check if the player is on the ground then ?
     
  4. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    Why do you want to use SimpleMove instead of Move (by the way, what is truly the difference between those...) ?
     
  5. Metal Head

    Metal Head

    Joined:
    Aug 14, 2010
    Posts:
    411
    I don't know...I just used simple move...

    I just used the Move func and run onto a problem. The function does not apply gravity to my Player, also my player runs awfully fast.
    -How would I apply the gravity to my player now?
    -Why does it run so fast?


    EDIT: Fixed the fast moving. Found out that this command moves the player by the distance, given per frame, not per second. Had to multiply it with deltaTime.

    Code (csharp):
    1. var speed = 5.0;
    2. var jumpspeed = 0;
    3. var jumped = false;
    4.  
    5. function Update () {
    6. transform.RotateAround (Vector3.up, Input.GetAxis ("Mouse X")/5);
    7. var controller : CharacterController = GetComponent(CharacterController);
    8. var moveDir = new Vector3(Input.GetAxis("Horizontal")*speed, 0, Input.GetAxis("Vertical")*speed);
    9. controller.SimpleMove(transform.TransformDirection(moveDir));
    10.  
    11. if(Input.GetKey("e"))
    12. {
    13. speed = 10.0;
    14. }
    15. else
    16. {
    17. speed = 5.0;
    18. }
    19.  
    20.  
    21. Physics.gravity = Vector3(0, -30, 0);
    22.  
    23. if(Input.GetKey("space"))
    24. {
    25. jumped = true;
    26. jumpspeed = 15;
    27. }
    28.  
    29. if(jumped)
    30. {
    31. if(jumpspeed > 0)
    32. {
    33. jumpspeed -= 5*Time.deltaTime;
    34. }
    35. else
    36. {
    37. jumped = false;
    38. }
    39.  
    40. transform.Translate(Vector3.up * jumpspeed * Time.deltaTime);
    41. }
    42.  
    43.  
    44. }
    And I think that the difference between SimpleMove and Move is the
     
  6. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    The gravity was in fact applied in the moveDirection.y :) When you jump, the component y (axe up/down) is suddenly boosted, to simulate a jump. It is quickly then decreased by little quantity. When it reach 0, your character do not move in the y axis anymore. When it falls under 0, your character falls then.
    It runs fast because it is frame dependent, and not time dependant. You must know about FPS (frame per second). A game usually runs at 30, or 60 fps. It means the game is updated 30 or 60 times per second.

    In your function Update, you move the CharacterController by 5 times the axis Y and X each time Update is called. If the game runs at 60 frames per second, Update is called 60 times. So, you will run 60 times 5. That's pretty huge.

    Simply multiply the value of move by Time.deltaTime. It is the time elapsed between the previous and the current frame.

    To make an image, let's say you have a cake. If your game runs a 4 frame per second, if you don't set the Time.deltaTime, when 4 frames will elapse, you would have eaten 4 cakes.

    If you multiply by Time.deltaTime (which should be around 0.25), you will eat 0.25 cake per frame.

    4 frames later, you ate 4*0.25 = 1 cake !

    That's how you bind actions to time. :wink:
     
  7. Metal Head

    Metal Head

    Joined:
    Aug 14, 2010
    Posts:
    411
    Thanks, I fixed the fast thing myself. I'm pretty familiar with the framerate dependance thing. I've done this for a long time with the previous engine I was working with (Genesis 3D), only there I had to make the variable myself, it wasn't build in the engine (god it was annoying). I figured that this command moves the object with amount per frame short after I posed my post, but I still can't figure out that moveDirection.y thing.

    Where is it used and how does it work? I mean, the moveDirection is translated in the Move function, but what does moveDirection.y mean?
     
  8. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    http://forum.unity3d.com/viewtopic.php?t=36096

    To put it simply, moveDirection is a variable of type Vector3. It is a set a three floats, x, y, and z. You can access them by writing vector.x, vector.y, or vector.z.

    For example, if you have var test : Vector3 (5,10,6); writing test.y will return you 10.

    So, moveDirection contains x, y, z informations. They are used to define where the CharacterController is heading.
     
  9. Glemau

    Glemau

    Joined:
    Jul 24, 2015
    Posts:
    37
    I think the main difference is, that SimpleMove adds and extra *Time.deltaTime in.(move(movementDirection * Time.deltaTime);
     
  10. Nitro00

    Nitro00

    Joined:
    Jan 11, 2018
    Posts:
    18
    You can check if it's grounded by simply using:

    CharacterController.isGrounded()
     
    MassimoFrancesco likes this.
  11. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,386
    It's been a couple years, I doubt they're still stuck on this.
     
    misterpoopybutthole likes this.
Thread Status:
Not open for further replies.