Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ZG_UCSC · Oct 20, 2016 at 08:14 PM · scripting problemplanemeshcollider

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:

alt text

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?

camera-rotation.jpg (313.5 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FlyingHighUp · Oct 20, 2016 at 08:31 PM 0
Share

On your cube rigidbody, try giving it a PhysX material and change the static and dynamic friction to 0. If that's okay.

avatar image OusedGames · Oct 20, 2016 at 09:08 PM 0
Share

$$anonymous$$aybe

  • Constrain Rotation no rigidBody

avatar image ZG_UCSC OusedGames · Oct 21, 2016 at 06:20 AM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZG_UCSC · Oct 21, 2016 at 06:17 AM 0
Share

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.

avatar image b1gry4n ZG_UCSC · Oct 21, 2016 at 06:37 AM 0
Share

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.

avatar image ZG_UCSC b1gry4n · Oct 21, 2016 at 07:27 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges