Strange Physics Bug

I'm making a game where a sled slides down a ramp. I got this weird error where the sled will tilt up into the air when sitting still. I tried disabling the PlayerController script and it worked just fine. The problem is that after I commented out ALL of the code in the PlayerController script, The bug was still there. Is it a problem with my code or not? Here's my code for reference:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
public float speed; // max movement speed (across the screen) in units/second
public float accel; // acceleration coeffiecient between current speed and target
private Rigidbody rb; // rigidbody
private float movement; //acceleration for movement
private float target; // target speed
private float speedDiff;
public SceneManager Game; // script on an object tilted so that its local space forward is down the ramp
public GameObject CenterOfMass;
public float bounds;
public float jump;

void Start()
{
rb = GetComponent();
rb.centerOfMass = CenterOfMass.transform.position;
}

void Update()
{
// player input left-right
/*
//if(Input.GetKey(KeyCode.LeftArrow))
//{
// if (transform.position.x < -bounds)
// target = .5f * speed;
// else
// target = -speed;
//}
//else if (Input.GetKey(KeyCode.RightArrow))
//{
// if (transform.position.x > bounds)
// target = .5f * -speed;
// else
// target = speed;
//}
//else
//{
// target = 0;
//}
// jump
//if (Input.GetKeyDown(KeyCode.UpArrow))
// rb.AddForce(jump * Game.Up(), ForceMode.Impulse);
//Move();
*/
}

private void OnCollisionStay(Collision other)
{
/*
//GameObject collision = other.collider.gameObject;
//if (collision.GetComponent().type == "Ground" && Vector3.Dot(other.GetContact(0).normal, Game.Up()) > .99f )
//{
// //rb.useGravity = false;
//}
//if (Vector3.Dot(other.GetContact(0).normal, -Game.Forward()) > .99f)
//{
// Game.Over();
//}
*/
}

private void OnCollisionExit(Collision other)
{
/*
//GameObject collision = other.collider.gameObject;
//if (collision.GetComponent().type == "Ground")
// {}rb.useGravity = true;
*/
}

// moves player left and right
//private void Move()
//{
// speedDiff = target - Vector3.Dot(rb.velocity, Game.Forward());
//
// if (speedDiff < 0.01 && speedDiff > -0.01)
// speedDiff = 0;
//
// movement = speedDiff * accel;
// rb.AddForce(movement * Game.Forward(), ForceMode.Acceleration);
//}
}

[quote=“ylhs-262912”, post:1, topic: 944454]
Here’s my code for reference:
[/quote]

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

I also question your center of mass settings. I think it should be a local position, not a global. That alone could make your nose point up in the air.

2 Likes

Rigidbody is my guess.

I removed both the center of mass and reset the rigidbody. Any other suggestions?

I don't quite get it (I think) but are you saying if you have the class (as we see it) with the code commented out you still see the problem? First off I'd recommend not commenting all the code out but opt for an if(false) test of some kind and you can toggle between it executing and not executing easily. You also don't need to be careful about the comments. Your private Move() isn't just commenting the code but the entire method. Probably not an issue but still the commented code tends to get in the way of understanding.

You also haven't used the "inert code" code option so reading it is generally a bit more difficult.

I found new info
When I remove the start function it works
problem: I need start


Try commenting out everything:

//using System.Collections;
//using System.Collections.Generic;
//using UnityEngine;
//public class PlayerController : MonoBehaviour
//{
//public float speed; // max movement speed (across the screen) in units/second
//public float accel; // acceleration coeffiecient between current speed and target
//private Rigidbody rb; // rigidbody
//private float movement; //acceleration for movement
//private float target; // target speed
//private float speedDiff;
//public SceneManager Game; // script on an object tilted so that its local space forward is down the ramp
//public GameObject CenterOfMass;
//public float bounds;
//public float jump;
//void Start()
//{
//rb = GetComponent<Rigidbody>();
//rb.centerOfMass = CenterOfMass.transform.position;
//}
//....
//}

Its the line for defining the center of mass
I tried moving the center of mass around, but no matter where I put it it still broke. Surprisingly no defined center of mass worked
This was 4 days of work for this sh*t


The fact that you assigned a world-position to the local center-of-mass is red-flag to me that there's misunderstanding here. The world center-of-mass is read-only. Transform.position is NOT local, it's global.

If you have a specific Transform.position (world-space) then you need to convert that into a position relative to the Transform the Rigidbody is on i.e. local-space. This is a basic Transform operation:

rb.centerOfMass = rb.transform.InverseTransformPoint(CenterOfMass.transform.position);

Note that this was pointed out in the first reply.

1 Like