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

No Gravity Jump

Discussion in 'Scripting' started by DrBibop, Jan 4, 2016.

  1. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    Hi,

    I'm trying to make a script so that the player jumps where the camera is facing, like he is jumping in a no gravity environement. I tried to use transform.foward but the problem is I don't want for the player to change the jump direction while he is jumping so that he can look around while jumping to a straight direction.

    Any ideas?
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If you only want the player to be able to change direction when they are on the ground do a check for isGrounded. If they are grounded they can turn, if they have jumped they won't be grounded so won't be able to turn.
     
  3. mumbot

    mumbot

    Joined:
    Oct 19, 2012
    Posts:
    86
    Thats what i would do so basically use the OnCollisionEnter() and OnCollisionExit() functions

    EDIT: And also there is the OnCollisionStay() function that can help you as well.
     
  4. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    It's a good idea. But I would like it if the player was able to look around in the air but keeps the same direction.
     
  5. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    When using OnCollisionEnter(), I can jump again through the wall (which is bad) and when using OnCollision Stay(), I'm stuck in the wall.
     
  6. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    FIXED : I used an Impulse force:

    Code (CSharp):
    1.         if (Input.GetKeyDown(KeyCode.Space) && IsGrounded == true)
    2.         {
    3.             IsGrounded = false;
    4.  
    5.             GetComponent<Rigidbody>().AddForce(transform.forward * 10, ForceMode.Impulse);
    6.         }