I’m currently trying to fix it so that when the cube is on the ground it can jump but after that cannot jump until it hits the ground again, however its just not jumping and am unsure why.
Here is my code:
public GameObject Box;
public Vector3 BoxRot;
public float BoxX;
public float BoxY;
public float BoxZ;
public float JumpHeight=100f;
public Vector3 JumpTo;
public GameObject floor;
public bool jumping=false;
public float CurX;
public string FloorTag = "Floor";
void OnCollisionEnter(Collision Col)
{
Debug.Log("collision started");
if (floor.gameObject.tag == FloorTag)
{
jumping = false;
Debug.Log("jumping=" + jumping.ToString());
}
}
void OnCollisionStay (Collision Col)
{
Debug.Log("colliding");
if (floor.gameObject.tag == FloorTag)
{
Debug.Log("jumping=" + jumping.ToString());
}
}
void OnCollisionExit (Collision Col)
{
Debug.Log("exiting");
if (floor.gameObject.tag == FloorTag)
{
jumping = true;
Debug.Log("jumping=" + jumping.ToString());
}
}
void Sideways()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
CurX = Box.transform.position.x;
Box.transform.position = Vector3.Lerp(new Vector3(CurX,Box.transform.position.y, Box.transform.position.z), new Vector3(CurX-35, Box.transform.position.y, Box.transform.position.z),2*Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
CurX = Box.transform.position.x;
Box.transform.position = Vector3.Lerp(new Vector3(CurX, Box.transform.position.y, Box.transform.position.z), new Vector3(CurX + 35, Box.transform.position.y, Box.transform.position.z), 2 * Time.deltaTime);
}
}
void Update ()
{
Debug.Log("jumping=" + jumping.ToString());
Sideways();
Box.transform.Translate (0,0, 10 * Time.deltaTime);
BoxY = Box.transform.position.y;
JumpTo = new Vector3 (0, JumpHeight, Box.transform.position.y);
if ((Input.GetKeyDown ("space"))&&(jumping=false))
{
Debug.Log("jumping=" + jumping.ToString());
Box.transform.Translate(Vector3.up * 100 * Time.deltaTime, Space.World);
}
}
}
Cherno
2
You don’t check if the object you collided with is a floor, you always check against the pre-defines floor object from your script.
This:
void OnCollisionEnter(Collision Col)
{
Debug.Log("collision started");
if (floor.gameObject.tag == FloorTag)
{
jumping = false;
Debug.Log("jumping=" + jumping.ToString());
}
}
Needs to be this:
void OnCollisionEnter(Collision Col)
{
Debug.Log("collision started");
if (Col.gameObject.tag == FloorTag)
{
jumping = false;
Debug.Log("jumping=" + jumping.ToString());
}
}
The same goes true for the other Collision function in your script.