I am currently trying out raycast hit for the first time.
I have create a simple scene with two boxes and a ball.
The ball has the ability to jump and double jump. All of this works fine and ray cast is detecting the plane floor and the boxes also when landing on top of them.
What i am finding however is if you catch the side of a box and roll onto it the raycast doesn’t appear to detect it has collided with anything.
Is there anyway around this or anything i need to change.: Code included below.
Thanks
public class PlayerMove : MonoBehaviour {
private Rigidbody rb;
public float Speed;
public float JumpHeight;
private bool grounded ;
private float disheight = 0.5f;
public int doubleJump = 0;
// Use this for initialization
void Start () {
grounded = true ;
rb = GetComponent<Rigidbody> ();
}
void Update()
{
RaycastHit hit;
Ray floorTouch = new Ray (transform.position, Vector3.down);
Debug.DrawRay (transform.position, Vector3.down * disheight);
if (Physics.Raycast (floorTouch, out hit, disheight)) {
grounded = true;
doubleJump = 0;
}
}
// Update is called once per frame
void FixedUpdate () {
float Hor = Input.GetAxis ("Horizontal");
float Ver = Input.GetAxis ("Vertical");
Vector3 Movement = new Vector3 (Hor, 0.0f, Ver);
rb.AddForce (Movement * Speed);
if (Input.GetKeyDown (KeyCode.Space) && ((grounded) || doubleJump < 2)) {
rb.AddForce (new Vector3(0,(JumpHeight * 5) ,0), ForceMode.Acceleration );
GetComponent<AudioSource>().Play();
doubleJump += 1;
grounded = false;
}
}
}