- Home /
How to prevent game object from rolling on a curved plane?
I have created a twisted plane by adjusting the vertices of the mesh in a plane object. I am trying to make other game object going over it smoothly, i.e. making full surface contact with the plane and with local y axis in the same direction as the plane's normal. Here is what I have for now:
As you might see from the image above, the game object, which is a cube, is rolling over the curved plane surface instead of sliding over it. How to prevent such undesired rolling? Here is the script I have for the cube now:
using UnityEngine;
using System.Collections;
public class CubeControl2 : MonoBehaviour {
private Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody>();
}
void Update () {
rb.velocity = new Vector3(0, 0, -1);
Vector3 gravity = new Vector3(-transform.localPosition.x, -transform.localPosition.y, 0);
rb.AddForce(gravity);
}
}
And here is the script I used to twist the plane:
void Awake()
{
Mesh PlaneMesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = PlaneMesh.vertices;
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
if (j < 5)
{
vertices[i * 11 + j].y = vertices[i * 11 + j].x * Mathf.Sin(Mathf.Deg2Rad * 18 * i);
vertices[i * 11 + j].x = vertices[i * 11 + j].x * Mathf.Cos(Mathf.Deg2Rad * 18 * i);
}
else if (j > 5)
{
vertices[i * 11 + j].y = vertices[i * 11 + j].x * Mathf.Sin(Mathf.Deg2Rad * 18 * i);
vertices[i * 11 + j].x = vertices[i * 11 + j].x * Mathf.Cos(Mathf.Deg2Rad * 18 * i);
}
}
}
PlaneMesh.vertices = vertices;
GetComponent<MeshCollider>().sharedMesh = PlaneMesh;
}
What should I change?
On your cube rigidbody, try giving it a PhysX material and change the static and dynamic friction to 0. If that's okay.
Constrain rotation in X and Y direction for the cube kind of worked. Thanks. But suppose I have a more complicated plane curvature then this could not be a option if I want to achieve smooth sliding effect. Any other suggestions?
Answer by Naphier · Oct 21, 2016 at 02:41 AM
Ultimately, rigidbody might not be the best option. I had to do something similar for a luge game. What I ended up doing is adding an anchor to my rigidbody to give it more weight below the surface. It might work just the same if you up the mass and gravity, also try changing the angular drag a bit higher.
Answer by b1gry4n · Oct 21, 2016 at 02:58 AM
The "slipping" is caused by gravity I am assuming. Gravity is a constant force (default is 0, -9.81, 0) so it will always be forcing objects in that direction. I am not sure of the exact way gravity is applied to rigidbodies, but when I use rigidbodies, i never use the default gravity. I always disable rigidbody gravity and create my own gravity, especially when I want complete control over an object in weird situations.
void FixedUpdate()
{
rigidBody.AddForce(Physics.gravity);
}
What you could do is use this same method, but change the direction of the force so that it is always pointing at the "point of contact". You could get the point of contact with a ray cast or a direct reference to the game object. Ill leave figuring that part out to you.
void FixedUpdate()
{
Vector3 dir = (transform.position - pointOfContact).normalized;
rigidBody.AddForce(dir * Physics.gravity.y);
}
If i have misunderstood your question and you want to prevent the square from simply tumbling over, dont let the rigidbody control rotation when the object is "grounded". Lock the rotation and rotate the rigidbody yourself. If the object isnt grounded, give control back to the rigidbody
I had turned off gravity for the cube when I was testing it. It seems to be the default behavior Unity has defined for objects encountering another inclined collider.
It doesnt have to be this complicated if you just want it to rotate to the surface. Cast a ray from the cube and rotate the cube to the hit normal.
I do not want the cube to roll, not preventing it from rotation. When the cube slides over the twisted plane inevitably it would rotate alone its sliding direction and that is what I want. So I merely do not want the cube to rotate in the direction I do not want it to.
Your answer
Follow this Question
Related Questions
Mesh triangles and vertices, searching for adjacent triangle 1 Answer
Get the triangles of a convex mesh collider 1 Answer
Can we Plant tree on Planes Instead of Terrain using Procedural Placement 0 Answers
Cannot add Mesh via script 1 Answer
Get point in plane where the normal crosses a GameObject in local space 2 Answers