ixTec
ixTec

Reputation: 107

Jump raycasting isGrounded not working properly since Unity 5

I asked a question a while back, regarding raycasting for making a player jump when and if they were touching the ground, I managed to receive great help and solved my issue. Since updating my project to Unity 5, my player no longer jumps and the if statement regarding it is never run:

if(Physics.Raycast(ray, rayDistance, 1 << 8))

I'm not sure why this happening and after going back to my last answered question, I can't solve this issue. If anyone could help I would be much appreciative as I'm still relatively new to Unity and C#. Thank you

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

// Update is called once per frame
void FixedUpdate() {

    // Creating floats to hold the speed of the plater
    float playerSpeedHorizontal = 4f * Input.GetAxis ("Horizontal");
    float playerSpeedVertical = 4f * Input.GetAxis ("Vertical");


    //Vector3 velocity = transform.forward * playerSpeedVertical + transform.right * playerSpeedHorizontal;
    //velocity.Normalize();
    //velocity *= playerMaxSpeed;
    //velocity.y = rigidbody.velocity.y;
    //rigidbody.velocity = velocity;

    // Transform statements to move the player by the playerSpeed amount.
    transform.Translate (Vector3.forward * playerSpeedVertical * Time.deltaTime);
    transform.Translate (Vector3.right * playerSpeedHorizontal * Time.deltaTime);

    // Calling the playerJump function when the jump key is pressed
    if (Input.GetButton("Jump"))
    {
        playerJump();
    }
}

/// Here we handle anything to do with the jump, including the raycast, any animations, and the force setting it's self.
void playerJump() {

    const float JumpForce = 1.75f;
    Debug.Log ("Should Jump");

    Vector3 rayOrigin = transform.position;
    rayOrigin.y += GetComponent<Collider>().bounds.extents.y; //move the ray origin up into the collider so that it won't begin in the ground / floor
    float rayDistance = GetComponent<Collider>().bounds.extents.y + 0.1f;

    Ray ray = new Ray ();
    ray.origin = rayOrigin;
    ray.direction = Vector3.down;

    if(Physics.Raycast(ray, rayDistance, 1 << 8)) {
        Debug.Log ("Jumping");
        GetComponent<Rigidbody>().AddForce (Vector3.up * JumpForce, ForceMode.VelocityChange);
    }
}
}

Upvotes: 0

Views: 3979

Answers (1)

Patrick
Patrick

Reputation: 36

Before, your code depended on the position of the gameobject and center of its bounds being the same. Its possible that when you updated to unity5 this may have changed, but there may be other causes.

The solution is to check from the center of the bounds, rather then from the center of the object (its position). Because you use bounds.extents to get the distance from the center of the bounds to the edge of the bounds, that would no longer work if the bounds center and the gameObjects center are different.

Here is the changed code that should do this:

/// Here we handle anything to do with the jump, including the raycast, any animations, and the force setting it's self.
void playerJump() {

    const float JumpForce = 1.75f;
    Debug.Log ("Should Jump");

    Vector3 rayOrigin = GetComponent<Collider>().bounds.center;

    float rayDistance = GetComponent<Collider>().bounds.extents.y + 0.1f;
    Ray ray = new Ray ();
    ray.origin = rayOrigin;
    ray.direction = Vector3.down;
    if(Physics.Raycast(ray, rayDistance, 1 << 8)) {
        Debug.Log ("Jumping");
        GetComponent<Rigidbody>().AddForce (Vector3.up * JumpForce, ForceMode.VelocityChange);
    }
}
}

Upvotes: 1

Related Questions