0

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

Fabian T
  • 1
  • 1
  • Using physics is often not the best solution for finicky setups. You could use the more precise transform for your general movement and check for collisions with obstacles using [Physics.OverlapBox](https://docs.unity3d.com/ScriptReference/Physics.OverlapBox.html). – Voidsay Oct 25 '22 at 13:54
  • So how would I use transform for movement if i want to have a smooth movement to the sides and to the front without sudden movements when hitting the button for moving to the side? This is why I used AddForce – Fabian T Oct 25 '22 at 14:41

2 Answers2

0

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);

Charles Lambert
  • 5,042
  • 26
  • 47
  • Thanks, but as I wrote in my last sentence, I already found the feature to freeze the Y Axis of the rigidbody, but it didin't help enough, because now the Player slows down instad of bouncing or flying away – Fabian T Oct 25 '22 at 13:21
0

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)

Voidsay
  • 1,462
  • 2
  • 3
  • 15
  • Thank you very much I will try this. But for today I had enough with this project... Had some problems with creating a new release for Google Play. But I think now it works – Fabian T Oct 25 '22 at 19:55