Why doesn't the collider work?
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);
}
}
}
I haven't checked the complete code, but first of all you should fix line 62:
if ((Input.Get$$anonymous$$eyDown ("space"))&&(jumping=false))
You missed a '=' there. It should read:
if (Input.Get$$anonymous$$eyDown ("space") && jumping==false)
or even better:
if (Input.Get$$anonymous$$eyDown ("space") && !jumping)
Answer by Cherno · Oct 01, 2015 at 09:19 PM
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.
OF COURSE! I make stupid mistakes like this all the time, the problem is I think I must have missed something as it still isn't working.
my code now looks like this:
public class $$anonymous$$ovement : $$anonymous$$onoBehaviour {
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 (Col.gameObject.tag == FloorTag)
{
jumping = false;
Debug.Log("jumping=" + jumping.ToString());
}
}
void OnCollisionStay (Collision Col1)
{
Debug.Log("colliding");
if (Col1.gameObject.tag == FloorTag)
{
Debug.Log("jumping=" + jumping.ToString());
}
}
void OnCollisionExit (Collision Col2)
{
Debug.Log("exiting");
if (Col2.gameObject.tag == FloorTag)
{
jumping = true;
Debug.Log("jumping=" + jumping.ToString());
}
}
void Sideways()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.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.Get$$anonymous$$eyDown($$anonymous$$eyCode.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.Get$$anonymous$$eyDown ("space") && !jumping)
{
Debug.Log("jumping=" + jumping.ToString());
Box.transform.Translate(Vector3.up * 100 * Time.deltaTime, Space.World);
}
}
}
never $$anonymous$$d I fixed it, thanks for the help bro
If you want to help others, I might want to say what you did to fix it :)
Your answer
Follow this Question
Related Questions
how do I make my character jump while running without losing all your speed 1 Answer
While (starting jumping or in jump) holding W key makes game object jump high and fall slow. 1 Answer
Why is my bool being set to true when program is run? 1 Answer
jump code character can jump twice please any one fix this 0 Answers
C# Code won't ket me jump 0 Answers