- Home /
 
Rotating child doesn't move parent Rigidbody?
https://gyazo.com/b5afbe010b6c3ff17e285ef1a7fc5d02
I feel like this is a common problem, but I can't find a easy way around it. I'm making a game kinda like Garry's mod and I've got rotator blocks which rotate whatever's attached to them. In the video, I added a large block onto these rotators, but it isn't moving the car. How can I get it to move? I've tried upping the friction, and I can't use wheelcolliders on the large block.
By the way, the way the car works is: The grey block in the middle has a rigidbody All the white blocks are parented to it, as well as the rotator blocks The big wheel shaped blocks are parented to the rotators
Since building cars and buildings and things is the point of the game, i can't easily add things like joints externally.
Thanks in advance!
Answer by Anis1808 · Dec 09, 2019 at 10:45 PM
Okay so from what I see the problem is that your blocks are not even moving forward so a simple but dangerous solution would be to add a velocity to the object. I say dangerous because it may result is strange behaviors so you would need to test it very carefully.
A second solution wold be to make the blocks move actually (this could be achieve by rotating the cubes around their edges as in the following function). They you would need to use a hinge joint to link the center block to the "wheels".
 public float rotationPeriod = 0.3f; // Time to move to the next
 public float sideLength = 0.5f; // Length of cube side
 bool isRotating = false; // Flag to detect whether the cube is rotating
 float directionX = 0; // Rotation direction flag
 float directionZ = 0; // Rotation direction flag
 Vector3 startPos; // Position of Cube before rotation
 float rotationTime = 0; // Time lapse during rotation
 float radius; // Orbit radius of center of gravity
 Quaternion fromRotation; // Cube quaternion before rotation
 Quaternion toRotation; // Cube quaternion after rotation
 
 void FixedUpdate () {
 
     if (isRotating) {
 
         rotationTime += Time.fixedDeltaTime; // increase elapsed time
         float ratio = Mathf.Lerp (0, 1, rotationTime / rotationPeriod); // ratio of current elapsed time to rotation time
 
         // move
         float thetaRad = Mathf.Lerp (0, Mathf.PI / 2f, ratio); // Rotation angle in radians.
         float distanceX = -directionX * radius * (Mathf.Cos (45f * Mathf. Deg2Rad)-Mathf. Cos (45f * Mathf. Deg2Rad + thetaRad)); // Distance traveled on the X axis. The sign of-matches the direction of movement with the key.
         float distanceY = radius * (Mathf.Sin (45f * Mathf.Deg2Rad + thetaRad)-Mathf.Sin (45f * Mathf.Deg2Rad)); // Y axis movement distance
         float distanceZ = directionZ * radius * (Mathf.Cos (45f * Mathf. Deg2Rad)-Mathf. Cos (45f * Mathf. Deg2Rad + thetaRad)); // Z-axis travel distance
         transform.position = new Vector3 (startPos.x + distanceX, startPos.y + distanceY, startPos.z + distanceZ); // set current position
 
         // rotation
         transform.rotation = Quaternion.Lerp (fromRotation, toRotation, ratio); 
         if (ratio == 1) {
             isRotating = false;
             directionX = 0;
             directionZ = 0;
             rotationTime = 0;
         }
     }
 }
 
               Calling this function would look like
 if (/*SOME INPUT*/ && !isRotating) {
     directionX = y; // rotation direction set (x or y is always 0)
     directionZ = x; // rotation direction set (x or y is always 0)
     startPos = transform.position; // Keep coordinates before rotation
     fromRotation = transform.rotation; // Hold quaternion before rotation
     transform.Rotate (directionZ * 90, 0, directionX * 90, Space.World); // Rotate 90 degrees in rotation direction
     toRotation = transform.rotation; // Hold quaternion after rotation
     transform.rotation = fromRotation; // Return the Cube's Rotation before rotation. (Can I do something like a shallow copy of transform ...)
     rotationTime = 0; // Elapsed time during rotation to 0.
     isRotating = true; // Turn on the rotation flag.
                 
 }
 
              Your answer