Search Unity

How to Make a JUMP in C#

Discussion in 'Scripting' started by baxter, Feb 2, 2010.

  1. baxter

    baxter

    Joined:
    Jul 4, 2009
    Posts:
    39
    hi i need to make a jump for my pg in C#...

    what i command code need to make it?

    can i see some examples?

    tnx! :wink:
     
  2. damien_oconnell

    damien_oconnell

    Joined:
    Dec 24, 2009
    Posts:
    58
    pg = program?
    You want to jump to another line of code in C#?
    You want to make a game object in your scene jump?
     
  3. baxter

    baxter

    Joined:
    Jul 4, 2009
    Posts:
    39
    eheheh, the second!
    i want to make a jump for my Character in a 3d platform! :D
     
  4. damien_oconnell

    damien_oconnell

    Joined:
    Dec 24, 2009
    Posts:
    58
    The First Person Controller in the standard assets package already implements a basic jump in the attached FPSWalker script. Are you looking to achieve something different?.
     
  5. baxter

    baxter

    Joined:
    Jul 4, 2009
    Posts:
    39
    no it's ok! but when i try to convert the script in c# unity say it not finished compilation yet!!Why?
    say this also with Wowcamera script....i don't know whi...
     
  6. damien_oconnell

    damien_oconnell

    Joined:
    Dec 24, 2009
    Posts:
    58
    You probably have errors in one or both of the scripts.

    Here is the FPSWalker in c#:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(CharacterController))]
    4.  
    5. public class FPSWalker : MonoBehaviour
    6. {
    7.     public float speed = 6.0f;
    8.     public float jumpSpeed = 8.0f;
    9.     public float gravity = 20.0f;
    10.  
    11.     private Vector3 moveDirection = Vector3.zero;
    12.     private bool grounded = false;
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         if (grounded)
    17.         {
    18.             // We are grounded, so recalculate movedirection directly from axes
    19.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    20.             moveDirection = transform.TransformDirection(moveDirection);
    21.             moveDirection *= speed;
    22.        
    23.             if (Input.GetButton ("Jump"))  moveDirection.y = jumpSpeed;
    24.         }
    25.  
    26.         // Apply gravity
    27.         moveDirection.y -= gravity * Time.deltaTime;
    28.    
    29.         // Move the controller
    30.         CharacterController controller = (CharacterController)GetComponent(typeof(CharacterController));
    31.         CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
    32.         grounded = (flags  CollisionFlags.CollidedBelow) != 0;
    33.     }
    34.  
    35.  
    36.  
    37. }
     
  7. baxter

    baxter

    Joined:
    Jul 4, 2009
    Posts:
    39
    tnx but with your script i got the same error.....is not finished compilation yet.... :cry:
     
  8. damien_oconnell

    damien_oconnell

    Joined:
    Dec 24, 2009
    Posts:
    58
    Unity is trying to compile all the scripts in your project one or more of your other scripts have errors in them, do you get any errors reported in console window?
     
  9. baxter

    baxter

    Joined:
    Jul 4, 2009
    Posts:
    39
    tnx but with your script i got the same error.....is not finished compilation yet.... :cry:
     
  10. baxter

    baxter

    Joined:
    Jul 4, 2009
    Posts:
    39
    oh very very tnx!!!!!!!!!!!!!there are many error in another script!but i don't know because the error in other script (NOT ATTACHED) are the cause of not working game....tnx so much!
     
  11. Owen Games

    Owen Games

    Joined:
    Aug 31, 2014
    Posts:
    2
    Its a good script but there is an error message of "error CS1525: Unexpected symbol 'CollisionFlags' " I dont know whats rong with it but there just is.
     
  12. Owen Games

    Owen Games

    Joined:
    Aug 31, 2014
    Posts:
    2
    Ow, and i have no idea how to code.
     
  13. Dk20_Omega

    Dk20_Omega

    Joined:
    Mar 21, 2014
    Posts:
    2
    i copyed the function (void ) form my movement script

    i hope is is what you wnated ?


    void jump(){
    Vector3 Motion;
    Motion = Vector3.zero;
    CharacterController controller;
    controller = gameObject.GetComponent<CharacterController>();
    //jump
    if(Input.GetKey(KeyCode.Space)|| controller.isGrounded)
    {
    Motion.y = jumpForce;
    }

    Motion.y -= gravity * Time.deltaTime;
    controller.Move(Motion * Time.deltaTime);
    }
     
  14. Kimajita

    Kimajita

    Joined:
    Nov 29, 2015
    Posts:
    1
    Thank you for trying but why is still a fault with me yet. o_O
     
  15. Noxury

    Noxury

    Joined:
    Mar 8, 2015
    Posts:
    21
    Don't use Input checking in FixedUpdate!
    This is a confusing and often misunderstood area. As a general rule:

    • Input should be in Update(), so that there is no chance of having a frame in which you miss the player input (which could happen if you placed it in FixedUpdate(), say).

    • Physics calculations should be in FixedUpdate(), so that they are consistent and synchronised with the global physics timestep of the game (by default 50 times per second).

    • Camera movement should be in LateUpdate(), so that it reflects the positions of any objects that may have moved in the current frame.
    However, there are always exceptions to these, and some experimentation may be required to get what works for you in a given scenario.- tanoshimi
     
  16. surmasanteri

    surmasanteri

    Joined:
    Nov 22, 2018
    Posts:
    1
    Hi! Here is very simple JUMP:

    Write before void start:
    (so you can change value later without touching code)

    public float upForce = 500f;

    Then write this in void FixedUpdate:

    void FixedUpdate()
    {
    if (Input.GetKey("space")) // If the player is pressing the "space" key player will move up.
    {
    // Add a jump force
    rb.AddForce(0, upForce * Time.deltaTime, 0);
    }

    }


    Hope it's helpful for some1.
    Thanks.
     
  17. kristianbossio06

    kristianbossio06

    Joined:
    Jan 8, 2020
    Posts:
    1
    [QUOTE = "surmasanteri, post: 3917722, membro: 2336341"] Ciao! Ecco JUMP molto semplice:

    Scrivi prima dell'inizio vuoto:
    (quindi puoi modificare il valore in un secondo momento senza toccare il codice)

    float pubblico upForce = 500f;

    Quindi scrivi questo nel nulla FixedUpdate:

    void FixedUpdate ()
    {
    if (Input.GetKey ("space")) // Se il giocatore sta premendo il tasto "spazio", il giocatore si sposterà verso l'alto.
    {
    // Aggiungi una forza di salto
    rb.AddForce (0, upForce * Time.deltaTime, 0);
    }

    }


    Spero sia utile per some1.
    Grazie. [/ QUOTE]
    Potresti mandare direttamente il codice? sono nabbo :)
     
  18. bekabo

    bekabo

    Joined:
    Dec 12, 2020
    Posts:
    1
    its still not working
     
  19. supersonicglitchy4

    supersonicglitchy4

    Joined:
    Aug 22, 2021
    Posts:
    2
    so i wrote this code for movement


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -19.81f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;
    private bool onGround;

    // Update is called once per frame
    void Update()
    {
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if (isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    if (Input.GetButtonDown("Jump") && isGrounded)
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);
    }
    }
    [/code]

    the movement on wasd works
    but the jumping doesnt work

    i even made all of the objects ground layer

    but still.
     
  20. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,304
    ... while at the same time failing to notice you are posting to a thread from 2010.

    When you have a problem, start your own thread, as per forum rules. It's FREE!

    Necroposting:


    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Use code tags:
     
unityunity