Home > OS >  Unity 3D: Cube crashes into non existing edges of automatically generated slippery plane (endless ru
Unity 3D: Cube crashes into non existing edges of automatically generated slippery plane (endless ru

Time:10-26

I have made an endless runner game. There, a cube is sliding on a slippery plane. Therefore I created multiple prefabs of planes with obstacles on it which are random placed one after another. Now, when I start the game and my player (a cube) slides on the planes, it sometimes seems to collide with the edges of a plane, which causes the cube to fly away.

private void Update(){
    if (rb != null){
        if (rb.position.y > 0.513f){
            pos = rb.transform.position;
            pos.y = 0.51f;
            rb.transform.position = pos;
        }
        rot = rb.transform.rotation;
        rot.y = 0;
        rot.x = 0;
        rot.z = 0;
        rb.transform.rotation = rot;
        if (rb.position.y > 1.01f)
            forwardForce = 2500f;
        else
            forwardForce = 5300f;
        if (Input.GetKey("d"))
            rb.AddForce(sidewayForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        if (Input.GetKey("a"))
            rb.AddForce(-sidewayForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
    }

Here, rb is the Rigidbody of the cube. And as you can see, I have tried to solve the problem by detecting if the player position is too high, and then putting it just slightly above the y-position of the plane, so that caused by possible bad floating point precision, it doesn't get stuck inside the plane. Now the cube doesn't fly away, but it kind of bounces very aggressively instead. And while writing this question I found out, that you can freeze the y-coordinate of the player, which helps a bit, but instead of bouncing it slows down now, which is also bad but better.

Edit: I 'fixed' it now by just letting the cube fly slightly above the platform. By that it doesn't collide with the edges or whatever the reason was

CodePudding user response:

In this case you may interest : https://docs.unity3d.com/ScriptReference/RigidbodyConstraints.html

You can freeze rigidbody position Y Axis.

if (rb.position.y > 0.513f){ pos = rb.transform.position; pos.y = 0.51f; rb.transform.position =pos; }

The rigidbody position is already equal to the position of the gameobject it's on, so you just need to get to the gameobject.transform.position here.

if(transform.position.y > yMaxValue) { transform.position = new Vector3(transform.position.x,targetYValue,transform.position.z);

CodePudding user response:

Instead of relying on the physics engine you can calculate only the essential things and gain more control.

Vector3 velocity;
float initialForwardSpeed;
LayerMask obstacleLayer;

void Start(){
  velocity = Vector3.forward*initialForwardSpeed;
}

private void Update(){
  if (Input.GetKey("d"))
    velocity  = new Vectro3(sidewayForce * Time.deltaTime, 0, 0);
  if (Input.GetKey("a"))
    velocity  = new Vectro3(-sidewayForce * Time.deltaTime, 0, 0);
  tranform.position  = velocity*Time.deltaTime;
  Collider[] hitColliders = Physics.OverlapBox(transform.position, Vector3.One*0.5f, transform.rotation, obstacleLayer);
  if(hitColliders.Length > 0){
    CollisionOccured();
  }
}

void CollisionOccured(){
  Debug.Log("Collided with obstacle");//You'll probably need this for your game logic
}

This code should move a cube of size 1 forward with the speed of initialForwardSpeed, allow for exactly the same lateral movement and will give you a message when you collide with an obstacle (object with a collider on the obstacle layer)

  • Related