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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Move character with Character Controller and Rigidbody

Discussion in 'Physics' started by AAcat, Jan 15, 2018.

  1. AAcat

    AAcat

    Joined:
    Apr 11, 2015
    Posts:
    19
    I'm trying to get a character to move on the world like any game would. However I wish for the character to only jump once (no infinite jump). I have never used Character Controller before, but once I figured it out, I tested it and the character flies across the screen like crazy.
    Assume it has been initialized correctly, the problem is between lines 25-30.
    Code (CSharp):
    1. void Update(){
    2.      
    3.         if(con.isGrounded){
    4.         //if (canJump) {
    5.             if (Input.GetButtonDown ("Jump")) {
    6.                 GetComponent<Rigidbody> ().velocity = Vector3.up * JumpSpeed;
    7.             }
    8.         }
    9.  
    10.     }
    11.  
    12.     void FixedUpdate(){
    13.          
    14.         float horz = Input.GetAxis ("Horizontal");
    15.         float vert = Input.GetAxis ("Vertical");
    16.  
    17.         //jump
    18.         if (rb.velocity.y < 0) {
    19.             rb.velocity += Vector3.up * Physics.gravity.y * (fallSpeed - 1) * Time.fixedDeltaTime;
    20.         }
    21.         else if (rb.velocity.y > 0 && !Input.GetButton ("Jump")){
    22.             rb.velocity += Vector3.up * Physics.gravity.y * (quickJump - 1) * Time.fixedDeltaTime;
    23.         }
    24.          
    25.         move = new Vector3 (horz, 0, vert); //I replace the float y value from 0 to physics.gravity.y to help variant 2, which makes the character fall faster, but the character ends up falling through the floor of the world still
    26.  
    27.         transform.Translate (move * speed * Time.fixedDeltaTime); //variant 1, works perfectly without Character Controller, but I need Character Controller. Moves super fast with Character Controller. Bounces off walls for some reason.
    28.  
    29.         con.Move(move * speed * Time.fixedDeltaTime);// variant 2, Moves too fast, doesn't fall fast, and falls through world floor. Movement is jittery as well
    30.  
    31.  
    32.     }
    I want it to work how variant 1 has it, but I need the Character Controller in there as well.

    NOTE: variant 1 and 2 are never running at the same time.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  3. AAcat

    AAcat

    Joined:
    Apr 11, 2015
    Posts:
    19
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool :)

    I'm sure there are many, many posts about char. controllers and rb character controllers, all over the forums. :)

    Simple movement & jump scripts, especially. Enjoy :)