learning how to code for the second time im building a simple endless runner block game. when the character collides with an object at sometimes it works fine sometimes either the character or the object disappears, i mostly notice it happening when the collision occurs while there is movement on the z axis
i have scripted the character to move forward left /right and back using ‘wsad’, the game should end and restart when it hits an object or villain(coaster is what i have named the villain) or if the character falls below a certain level. the console will show that there is constant nan errors and that the object is moving too far from the world, but in reality the object just disappeared and seems to no longer exist, followed by the game restarting unless the character collides with the side of the ground while falling causing it to disappear before registering that it has gone below the level.
this is the collision script
public class Playercollision : MonoBehaviour {
public Movement movement;
void OnCollisionEnter (Collision collinfo)
{
if (collinfo.collider.tag == "obsticle"
)
{
Debug.Log("oh rass");
GetComponent<Movement>().enabled = false;
FindObjectOfType<GameManager>().EndGame();
}
if (collinfo.collider.tag == "coaster"
)
{
Debug.Log("oh rass");
GetComponent<Movement>().enabled = false;
FindObjectOfType<GameManager>().EndGame();
}
}
}
this is the movement script
public class Movement : MonoBehaviour
{
// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float groundlev = -1f;
public float forwardForce = 500f; // Variable that determines the forward force
public float sidewaysForce = 500f; // Variable that determines the sideways force
public float upwardforce = 500f;
// We marked this as "Fixed"Update because we
// are using it to mess with physics.
void FixedUpdate()
{
// Add a forward force
if (Input.GetKey("w")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(forwardForce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("a"))
{
rb.AddForce(0, 0, sidewaysForce * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey("d"))
{
rb.AddForce(0, 0, -sidewaysForce * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey("space"))
{
rb.AddForce(0, upwardforce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("s")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-forwardForce * Time.deltaTime, 0, 0);
}
if (rb.position.y < groundlev)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}