I have a character that is always precisely grounded. Meaning it’s bottom is always exactly positioned at the top of an object. This is important, because it may be causing the problem. The problem is that my raycast goes through the top face of the cube, and hits the bottom face. I am trying to make jumping. Any help apreciated.
EDIT
Its not going through the face, i just thought it was because of the way drawray was appearing. However, its only allowing me to jump again when facing certain ways, i think the geometry of the capsule just makes it too far from the ground or something when facing certain ways. Then again, that doesnt make sense, because i set the raycast distance (for a test) to 0.25f, and it still did the same thing.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private float mouseX;
private float mouseY;
public float xSensitiv;
public float ySensitiv;
public bool canJump;
public float jumpForce;
void FixedUpdate () {
Debug.DrawRay (transform.position + Vector3.down, Vector3.down, Color.red, 100);
if (Input.GetKey (KeyCode.W))
{
GetComponent <Rigidbody> ().position += transform.forward * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.A))
{
GetComponent <Rigidbody> ().position -= transform.right * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.S))
{
GetComponent <Rigidbody> ().position -= transform.forward * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.D))
{
GetComponent <Rigidbody> ().position += transform.right * speed * Time.deltaTime;
}
mouseX += Input.GetAxis ("Mouse X") * xSensitiv * Time.deltaTime;
GetComponent <Rigidbody> ().rotation = Quaternion.Euler (0, mouseX, 0);
mouseY -= Input.GetAxis ("Mouse Y") * ySensitiv * Time.deltaTime;
mouseY = Mathf.Clamp (mouseY, -90l, 90);
Camera.main.transform.localRotation = Quaternion.Euler (mouseY, 0, 0);
if (Input.GetKey (KeyCode.Space) && canJump == true && Physics.Raycast (transform.position + Vector3.down, Vector3.down, 0.25f))
{
GetComponent <Rigidbody> ().velocity += Vector3.up * jumpForce;
}
}
}