- Home /
Rotation along edge of a cube around another cube as its planet
hey guys , i am trying to create a replica of Fit in the hole , currently i have some basic movement down with this code using UnityEngine; using System.Collections; using System.Collections.Generic; public class Cube : MonoBehaviour { public float tumblingDuration = 0.2f; void Update() { var dir = Vector3.zero; if (Input.GetKey(KeyCode.UpArrow)) dir = Vector3.forward; if (Input.GetKey(KeyCode.DownArrow)) dir = Vector3.back; if (Input.GetKey(KeyCode.LeftArrow)) dir = Vector3.left; if (Input.GetKey(KeyCode.RightArrow)) dir = Vector3.right; if (dir != Vector3.zero && !isTumbling) { StartCoroutine(Tumble(dir)); } } bool isTumbling = false; IEnumerator Tumble(Vector3 direction) { isTumbling = true; var rotAxis = Vector3.Cross(Vector3.up, direction); var pivot = (transform.position + Vector3.down * 0.5f) + direction * 0.5f; var startRotation = transform.rotation; var endRotation = Quaternion.AngleAxis(90.0f, rotAxis) * startRotation; var startPosition = transform.position; var endPosition = transform.position + direction; var rotSpeed = 90.0f / tumblingDuration; var t = 0.0f; while (t < tumblingDuration) { t += Time.deltaTime; if (t < tumblingDuration) { transform.RotateAround(pivot, rotAxis, rotSpeed * Time.deltaTime); yield return null; } else { transform.rotation = endRotation; transform.position = endPosition; } } isTumbling = false; } }
but its only rotate in one plane and reamins same .how can i change plane axis when i arrive at a edge and rotate wrt to that edge i thought of a solution with raycasting this is what i thought of but i dont have enough knowledge about raycast and how to implement in this mechanics anybody have suggestion on what code and what kind of system should i create please answer
Answer by madks13 · Sep 26, 2018 at 12:04 PM
You scheme/drawing is a bit confusing, might want to add notations.
Unity already provides localised space where you can translate and rotate objects in their own space instead of world space.
I suggest you look at parent/child relations of scene objects to begin with. From what i understood, it seems to be able to do the job. You might not even need to change your code much.
Your answer
![](https://koobas.hobune.stream/wayback/20220612175255im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Distribute terrain in zones 3 Answers
Help finding out which side of a cube a raycast hits 1 Answer
Multiple Cars not working 1 Answer
Detect face of cube clicked 4 Answers