c# - Jump raycasting isGrounded not working properly since Unity 5 -
i asked question while back, regarding raycasting making player jump when , if touching ground, managed receive great , solved issue. since updating project unity 5, player no longer jumps , if statement regarding never run:
if(physics.raycast(ray, raydistance, 1 << 8))
i'm not sure why happening , after going last answered question, can't solve issue. if appreciative i'm still relatively new unity , c#. thank you
using unityengine; using system.collections; public class playercontroller : monobehaviour { // update called once per frame void fixedupdate() { // creating floats hold speed of 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 move player playerspeed amount. transform.translate (vector3.forward * playerspeedvertical * time.deltatime); transform.translate (vector3.right * playerspeedhorizontal * time.deltatime); // calling playerjump function when jump key pressed if (input.getbutton("jump")) { playerjump(); } } /// here handle jump, including raycast, animations, , 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 ray origin collider won't begin in 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); } } }
before, code depended on position of gameobject , center of bounds being same. possible when updated unity5 may have changed, there may other causes.
the solution check center of bounds, rather center of object (its position). because use bounds.extents distance center of bounds edge of bounds, no longer work if bounds center , gameobjects center different.
here changed code should this:
/// here handle jump, including raycast, animations, , 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); } } }
Comments
Post a Comment