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

another rigidbody jumping problem c#

Discussion in 'Scripting' started by jaasso, Jun 20, 2013.

  1. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    what do i need to add to make the raycast's collision with the ground enable/disable jumping? currently i can jump off a wall which is not wanted. i searched and found some discussion around this same subject and similar stuff but couldnt figure out a solution

    pretty new to this, what ive done is basically copy some code here and there from tutorials, modify it a little and learn analyzing it and checking out some c# language tutorials, i do understand pretty much all the code ive posted here, just cant figure out how to use that raycast's collision any further than console messages. and hours of trial and error trying all kinds of things with the code in question with slim results.. would rock if anybody could throw some tips here, might help me realize some new stuff, having kind of a hard time writing own code still

    raycast script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class raycast : MonoBehaviour
    5. {
    6.    
    7.     public float rayLength = 1f;
    8.    
    9.    
    10.    
    11.     public void Update ()
    12.     {
    13.         RaycastHit hit;
    14.         if (Physics.Raycast(transform.position, -transform.up, out hit, rayLength))
    15.         {
    16.             if (hit.collider.gameObject.tag == "ground")
    17.             {
    18.                 print ("xxx");
    19.             }
    20.         }
    21.    
    22.     }
    23. }
    24.  


    characterControl script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof (Rigidbody))]
    5. [RequireComponent (typeof (CapsuleCollider))]
    6.  
    7.  
    8.  
    9. public class characterControls : MonoBehaviour
    10. {
    11.    
    12.     public float turnSpeed = 90f;
    13.     public float walkSpeed = 5f;
    14.     public float runSpeed = 10f;
    15.    
    16.     protected bool running;
    17.  
    18.     public float gravity = 10.0f;
    19.     public float maxVelocityChange = 10.0f;
    20.     public bool canJump = true;
    21.     public float jumpHeight = 2.0f;
    22.     private bool grounded = false;
    23.    
    24.  
    25.    
    26.     void Awake ()
    27.     {
    28.         rigidbody.freezeRotation = true;
    29.         rigidbody.useGravity = false;
    30.     }
    31.  
    32.    
    33.    
    34.     void FixedUpdate ()
    35.     {
    36.                
    37.         transform.Rotate (0f, Input.GetAxis ("Mouse X") * turnSpeed * Time.deltaTime, 0f);
    38.        
    39.         if (grounded)
    40.         {
    41.             Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    42.             targetVelocity = transform.TransformDirection(targetVelocity);
    43.             targetVelocity *= walkSpeed;
    44.            
    45.             running = Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift);
    46.             if (running) targetVelocity *= runSpeed;
    47.            
    48.  
    49.             Vector3 velocity = rigidbody.velocity;
    50.             Vector3 velocityChange = (targetVelocity - velocity);
    51.             velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    52.             velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    53.             velocityChange.y = 0;
    54.             rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
    55.            
    56.            
    57.             //jump
    58.             if (canJump  Input.GetKey(KeyCode.Space))
    59.             {
    60.                 rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    61.             }          
    62.         }
    63.  
    64.         rigidbody.AddForce(new Vector3 (0, -gravity * rigidbody.mass, 0));
    65.  
    66.         grounded = false;
    67.     }
    68.  
    69.    
    70.    
    71.     void OnCollisionStay ()
    72.     {
    73.         grounded = true;
    74.     }
    75.    
    76.    
    77.  
    78.     float CalculateJumpVerticalSpeed ()
    79.     {      
    80.         return Mathf.Sqrt(2 * jumpHeight * gravity);
    81.     }
    82. }
    83.  
    84.  
    85.  
     
    Last edited: Jun 20, 2013
  2. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    first of all, your canJump variable is not set to false after jumping first time and is in mid-air or whatever. when you jump set your canJump to false:
    Code (csharp):
    1.  
    2.   //jump
    3. if (canJump  Input.GetKey(KeyCode.Space))
    4. {
    5.      rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    6.      canJump = false;
    7. }    
    8.  
    Then when the player touches the ground again set it to true.
    and replace this:
    Code (csharp):
    1.  
    2. void OnCollisionStay ()
    3.     {
    4.         grounded = true;
    5.     }
    6.  
    with this
    Code (csharp):
    1.  
    2. void OnCollisionEnter (Collision other)
    3. {
    4.     if(other.gameObject.layer == ground_layer)
    5.     {
    6.         grounded = true;
    7.         canJump = true;
    8.     }
    9. }
    10.  
     
    Last edited: Jun 20, 2013
  3. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    thanks, it works a little different now, i'll play around with it, any additional tips etc more than welcome