- Home /
Rotate table on its edges like in Bloxorz
Hello! I'm trying to create a Bloxorz-like movement, but instead of a cuboid, I'm using a table. Everything works great until the table reaches this point, when it sinks through the plane.
The model was created directly in Unity, no third-party softwares were used, such as Blender. Here is the script in C# the table is using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum Direction {
North,
South,
East,
West
}
public class TableMove : MonoBehaviour {
private float rotationSpeed = 750f, totalRotation;
private bool isMoving;
private Direction rotationDirection;
private Vector3 pivot, axis, scale;
void Start () {
isMoving = false;
scale = transform.localScale / 2f;
}
void Update () {
if (isMoving) {
float deltaRotation = rotationSpeed * Time.deltaTime;
if (totalRotation + deltaRotation >= 90f) {
deltaRotation = 90f - totalRotation;
isMoving = false;
}
if ((rotationDirection == Direction.West) || (rotationDirection == Direction.North))
transform.RotateAround (pivot, axis, deltaRotation);
else transform.RotateAround (pivot, axis, -deltaRotation);
totalRotation += deltaRotation;
}
else if (Input.GetKeyDown (KeyCode.W)) Rotate (Direction.North);
else if (Input.GetKeyDown (KeyCode.A)) Rotate (Direction.West);
else if (Input.GetKeyDown (KeyCode.S)) Rotate (Direction.South);
else if (Input.GetKeyDown (KeyCode.D)) Rotate (Direction.East);
}
void Rotate (Direction direction) {
rotationDirection = direction;
isMoving = true;
totalRotation = 0f;
switch (rotationDirection) {
case Direction.East:
pivot = transform.position + new Vector3 (scale.x, -scale.y, 0);
break;
case Direction.West:
pivot = transform.position + new Vector3 (-scale.x, -scale.y, 0);
break;
case Direction.North:
if (transform.localEulerAngles.x != 0) {
if (transform.localEulerAngles.x == -90) {
pivot = transform.position + new Vector3 (0, -scale.y, scale.z + 0.9f);
} else {
pivot = transform.position + new Vector3 (0, -scale.y, scale.z);
}
}
else
pivot = transform.position + new Vector3 (0, -scale.y - 0.9f, scale.z);
break;
case Direction.South:
pivot = transform.position + new Vector3 (0, -scale.y, -scale.z);
break;
}
if ((rotationDirection == Direction.East) || (rotationDirection == Direction.West)) {
axis = Vector3.forward;
float temp = scale.x;
scale.x = scale.y;
scale.y = temp;
} else {
axis = Vector3.right;
float temp = scale.z;
scale.z = scale.y;
scale.y = temp;
}
}
}
Any help would be highly appreciated! :)
Sincerely,
Unity Passionate
Answer by Roger_0123 · Jun 13, 2020 at 03:29 PM
Maybe you need to change the pivot to the center of the object?
This script is supposed to change the pivot according to the direction you want the gameobject to go to, meaning that the pivot isn't static. It works well with the cuboid, but since table doesn't have bottom edges, just legs, it makes it quite difficult. How would you do it by placing the pivot to the center of the gameobject?