The question is answered, right answer was accepted
Problem with raycasting from center of the collider
Hello,
I've got a problem with my game in unity.
I created a object with a box collider, and a player with a capsule collider. I created a function which returns wether the player is grounded or not using raycasting.
Now when the player is on top of the box collider it should show an idle animation. But this animation only shows up when the center of the collider is right above the box collider. When the collider's center is not above the box collider it will show a falling animation, but the player isn't falling because the collider is still colliding with the box collider.
Here's the code for the player:
#pragma strict
var speed : float;
var jumpPower : float;
var disttoground : float;
var inair : boolean;
function Start () {
disttoground = GetComponent.<Collider>().bounds.extents.y;
}
function isGrounded() : boolean {
return Physics.Raycast(transform.position, -Vector3.up,disttoground );
}
function Update () {
if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)){
if(Input.GetKey(KeyCode.LeftShift)){
transform.Translate(Vector3.right * speed * 3 * Time.deltaTime);
GetComponent.<Animation>().CrossFade("Running");
}else{
transform.Translate(Vector3.right * speed * Time.deltaTime);
GetComponent.<Animation>().CrossFade("Walking");
}
}else if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)){
transform.Translate(Vector3.left * speed/2 * Time.deltaTime);
}else{
GetComponent.<Animation>().CrossFade("Idle");
}
//Jump code
if(Input.GetKeyUp(KeyCode.Space) && isGrounded()){
GetComponent.<Rigidbody>().AddForce(transform.up * jumpPower, ForceMode.Force);
}
if(! isGrounded()){
GetComponent.<Animation>().Play("Jumping");
}
}
I hope you can help me :)
-Teddynieuws
Answer by Teddynieuws · Aug 31, 2015 at 02:13 PM
I managed to fix it by not using a raycast but a capsule cast.