- Home /
enabling mesh collider for rigidbody screws up everything
Hi. I have dummy rigidbody gameobject with disabled convex mesh collider, and some other stuff. Everything is good. I changed colliders matrix in project settings so my collider will not collide with anything in test. I have some code to add forces to rigidbody(thrust and torque). Literally everything works fine, until i enable collider - rigidbody almost stops receiving torque that applied from script(with collider enabled it has like 100x drag instead), and starts rotating by X and Z, while X and Z are frozen in constraints. I have no idea why enabling collider ruins everything. I made video: youtube. Here is my code of movement script.
public class MController : MonoBehaviour
{
public Rigidbody rigidbodyDummy;
public Transform pointer;
public Properties props;
//private Quaternion targetDirection;
private Vector3 targetPoint;
private bool isRotation = false;
private bool isMovement = false;
private bool isManeuvering = false;
private float rotDelta = 0f;
private bool isRotatingRight;
public float distance = 0f;
private float angVelocity;
private float ex;
public float velocity;
public void CommandMoveTo(Vector3 point)
{
isManeuvering = true;
isRotation = true;
isMovement = true;
targetPoint = point;
//UpdateBrakes();
//UpdateAngBrakes();
ForceBrakes();
}
public void CommandRotateTo(Vector3 point)
{
isManeuvering = true;
isRotation = true;
isMovement = false;
targetPoint = point;
//UpdateAngBrakes();
ForceBrakes();
}
void Start()
{
rigidbodyDummy.transform.SetParent(null);
}
private void FixedUpdate()
{
velocity = rigidbodyDummy.velocity.magnitude;
velocity = Mathf.Round(velocity * 100f) / 100f;
angVelocity = rigidbodyDummy.angularVelocity.y * Mathf.Rad2Deg;
angVelocity = Mathf.Round(angVelocity * 100f) / 100f;
angVelocity = Mathf.Abs(angVelocity);
if (velocity != 0f || angVelocity != 0f)
{
transform.position = rigidbodyDummy.transform.position;
transform.rotation = rigidbodyDummy.transform.rotation;
}
if (!isManeuvering)
{
return;
}
if (isRotation)
{
TryRotate();
}
if (isMovement)
{
TryMove();
}
}
private void TryRotate()
{
Quaternion direction = Quaternion.LookRotation(targetPoint - rigidbodyDummy.transform.position); // get direction to point desired from current position
rotDelta = Quaternion.Angle(rigidbodyDummy.transform.rotation, direction); // set initial rotation delta
UpdateIsRotatingRight(direction);
if (rotDelta <= 0.1f)
{
rigidbodyDummy.transform.rotation = direction;
rigidbodyDummy.angularVelocity = Vector3.zero;
if (isMovement == false)
{
isRotation = false;
isManeuvering = false;
props.SetThrustFlame(0f, 4);
}
UpdateAngBrakes();
return;
}
if (isRotatingRight)
{
rigidbodyDummy.AddRelativeTorque(Vector3.up * props.GetTorque() * GetTorqueMult());
props.SetThrustFlame(GetTorqueMult(), 0);
}
else
{
rigidbodyDummy.AddRelativeTorque(Vector3.up * -props.GetTorque() * GetTorqueMult());
props.SetThrustFlame(GetTorqueMult(), 1);
}
UpdateAngBrakes();
}
private void UpdateIsRotatingRight(Quaternion direction)
{
pointer.transform.rotation = rigidbodyDummy.transform.rotation;
if (rotDelta < 165f)
{
pointer.Rotate(Vector3.up * 0.01f);
if (rotDelta < Quaternion.Angle(pointer.rotation, direction))
{
isRotatingRight = false;
}
else
{
isRotatingRight = true;
}
}
else
{
pointer.transform.Rotate(Vector3.up * -0.01f);
if (rotDelta < Quaternion.Angle(pointer.rotation, direction))
{
isRotatingRight = true;
}
else
{
isRotatingRight = false;
}
}
pointer.transform.rotation = rigidbodyDummy.transform.rotation;
}
private float GetTorqueMult()
{
if(rotDelta > 15f)
{
return 1f;
}
else if(rotDelta > 0f)
{
float mult = rotDelta / 15f;
mult = Mathf.Clamp(mult, 0.1f, 1f);
return mult;
}
else
{
return 0f;
}
}
private void UpdateAngBrakes()
{
if (rotDelta < 0.1f)
{
rigidbodyDummy.angularDrag = props.dragBase;
return;
}
if (rotDelta > angVelocity)
{
rigidbodyDummy.angularDrag = 1f;
}
else
{
float newDrag = angVelocity * 0.5f;
rigidbodyDummy.angularDrag = props.dragBase < newDrag ? props.dragBase : newDrag;
}
}
private void TryMove()
{
distance = Vector3.Distance(rigidbodyDummy.transform.position, targetPoint);
if (distance <= 1.0f)
{
isManeuvering = false;
isMovement = false;
isRotation = false;
props.SetThrustFlame(0f, 4);
UpdateBrakes();
return;
}
if (rotDelta <= props.maxRotDeltaForMove)
{
rigidbodyDummy.AddRelativeForce(Vector3.forward * props.maxThrust * GetThrustMult());
props.SetThrustFlame(GetThrustMult(), 2);
UpdateBrakes();
}
}
private float GetThrustMult()
{
if (distance > 10f)
{
return 1f;
}
else if (distance > 0f)
{
float mult = distance / 10f;
mult = Mathf.Clamp(mult, 0.01f, 1f);
return mult;
}
return 0f;
}
private void UpdateBrakes()
{
if (distance < 1.0f)
{
rigidbodyDummy.drag = props.dragBase;
rigidbodyDummy.angularDrag = props.dragBase;
return;
}
if (distance > velocity)
{
rigidbodyDummy.drag = 1f;
}
else
{
float newDrag = velocity * 0.5f;
rigidbodyDummy.drag = props.dragBase < newDrag ? props.dragBase : newDrag;
}
}
private void ForceBrakes()
{
rigidbodyDummy.drag = 1f;
rigidbodyDummy.angularDrag = 1f;
}
}
Your answer
Follow this Question
Related Questions
HELP ME!! importing stuff!!! 2 Answers
Rigidbody projectile gets stuck in two-sided Procedural mesh / mesh collider 0 Answers
Question on Collider Resource Consumption 0 Answers
Wheel has no friction on mesh collider of another GameObject. 0 Answers
Mesh Collider won't draw the mesh I am telling it to? 1 Answer