(Solved, see below)
I am trying to rotate a tile to face backwards (on first click) and then rotate it back on the second click.
This seems to work with single-axis rotations, however, the object does not always have the same rotation after the “return” if more than a single axis rotation is performed. Any help is appreciated.
Please Ignore SetGlyph().
Original Script:
using System.Linq;
using UnityEngine;
using DG.Tweening;
public class PoolGlyphController : MonoBehaviour {
public const float ScaleTime = 0.1f;
public const float RotationTime = 1f;
public const float ScaleBeforeRotation = 0.8f;
[HideInInspector]
public GameObject RenderGlyph;
private bool _canRotate = true;
private bool _facingBackwards;
private static readonly Vector3[] RotVects =
{
new Vector3(180f,0f,0f),
new Vector3(-180f,0f,0f),
new Vector3(0f,180f,0f),
new Vector3(0f,-180f,0f),
new Vector3(0f,-180f,-90f),
new Vector3(0f,180f,-90f),
new Vector3(180f,0f,-90f),
new Vector3(-180f,0f,-90f)
};
private Vector3 _returnVect;
void OnMouseDown()
{
LimitedRotation(0f);
}
public void LimitedRotation(float tdelay) {
if (_canRotate) {
if (!_facingBackwards) {
DoRotate(RotVects[Random.Range(0, RotVects.Length)],tdelay);
_facingBackwards = true;
}
else {
GlyphManager.glyphManager.SelectRandomColor();
GlyphManager.glyphManager.SelectRandomGlyph();
SetGlyph();
DoRotate(_returnVect,0f);
_facingBackwards = false;
}
}
}
void DoRotate(Vector3 rotationVect, float rDelay) {
if (!_canRotate)
return;
_canRotate = false;
Sequence blockRotate = DOTween.Sequence().Append(
transform.DOScale(new Vector3(ScaleBeforeRotation, ScaleBeforeRotation, ScaleBeforeRotation), ScaleTime).SetDelay(rDelay).SetEase(Ease.InCirc));
blockRotate.Append(
transform.DOLocalRotate(rotationVect, RotationTime, RotateMode.LocalAxisAdd));
blockRotate.Append(
transform.DOScale(new Vector3(1f, 1f, 1f), ScaleTime).SetEase(Ease.InCirc).OnComplete(() => { _canRotate = true; _returnVect = -rotationVect; }));
}
void SetGlyph()
{
if (RenderGlyph != null) {
RenderGlyph.GetComponent<MeshFilter>().sharedMesh = GlyphManager.glyphManager.SelectedGlyph.GetComponent<MeshFilter>().sharedMesh;
RenderGlyph.GetComponent<MeshRenderer>().material.SetColor("_Color", GlyphManager.glyphManager.SelectedGlyphColor.color);
GetComponent<MeshRenderer>().material = GlyphManager.glyphManager.BlockMaterials.Single(t => t.name == GlyphManager.glyphManager.SelectedGlyphColor.name);
return;
}
RenderGlyph = Instantiate(GlyphManager.glyphManager.SelectedGlyph, transform);
RenderGlyph.transform.localPosition = new Vector3(0f, 0f, 1f);
RenderGlyph.transform.localRotation = transform.localRotation;//Quaternion.Euler(-180f, 0f, 0f);
RenderGlyph.GetComponent<MeshRenderer>().material.SetColor("_Color", GlyphManager.glyphManager.SelectedGlyphColor.color);
GetComponent<MeshRenderer>().material = GlyphManager.glyphManager.BlockMaterials.Single(t => t.name == GlyphManager.glyphManager.SelectedGlyphColor.name);
}
}
Fixed Script:
using System.Linq;
using UnityEngine;
using DG.Tweening;
public class PoolGlyphController : MonoBehaviour {
public const float ScaleTime = 0.1f;
public const float RotationTime = 1f;
public const float ScaleBeforeRotation = 0.8f;
[HideInInspector]
public GameObject RenderGlyph;
private bool _canRotate = true;
private bool _facingBackwards;
private static readonly Vector3[] RotVects =
{
new Vector3(180f,0f,0f),
new Vector3(-180f,0f,0f),
new Vector3(0f,180f,0f),
new Vector3(0f,-180f,0f),
new Vector3(0f,-180f,-180f),
new Vector3(0f,180f,-180f),
new Vector3(180f,0f,-180f),
new Vector3(-180f,0f,-180f)
};
private Vector3 _returnVect;
void OnMouseDown() {
LimitedRotation(0f);
}
public void LimitedRotation(float tdelay) {
if (_canRotate) {
if (!_facingBackwards) {
DoRotate(RotVects[Random.Range(0, RotVects.Length)], tdelay);
_facingBackwards = true;
}
else {
GlyphManager.glyphManager.SelectRandomColor();
GlyphManager.glyphManager.SelectRandomGlyph();
SetGlyph();
DoRotate(_returnVect, 0f);
_facingBackwards = false;
}
}
}
void DoRotate(Vector3 rotationVect, float rDelay) {
if (!_canRotate)
return;
_canRotate = false;
Sequence blockRotate = DOTween.Sequence().Append(
transform.DOScale(new Vector3(ScaleBeforeRotation, ScaleBeforeRotation, ScaleBeforeRotation), ScaleTime).SetDelay(rDelay).SetEase(Ease.InCirc));
blockRotate.Append(
transform.DOLocalRotate(rotationVect, RotationTime, RotateMode.LocalAxisAdd));
blockRotate.Append(
transform.DOScale(new Vector3(1f, 1f, 1f), ScaleTime).SetEase(Ease.InCirc).OnComplete(() =>
{
_canRotate = true;
_returnVect = -rotationVect;
}));
}
void SetGlyph() {
if (RenderGlyph != null) {
RenderGlyph.GetComponent<MeshFilter>().sharedMesh = GlyphManager.glyphManager.SelectedGlyph.GetComponent<MeshFilter>().sharedMesh;
RenderGlyph.GetComponent<MeshRenderer>().material.SetColor("_Color", GlyphManager.glyphManager.SelectedGlyphColor.color);
GetComponent<MeshRenderer>().material = GlyphManager.glyphManager.BlockMaterials.Single(t => t.name == GlyphManager.glyphManager.SelectedGlyphColor.name);
return;
}
RenderGlyph = Instantiate(GlyphManager.glyphManager.SelectedGlyph, transform);
RenderGlyph.transform.localPosition = new Vector3(0f, 0f, 1f);
RenderGlyph.transform.rotation = Quaternion.Euler(_returnVect);
RenderGlyph.GetComponent<MeshRenderer>().material.SetColor("_Color", GlyphManager.glyphManager.SelectedGlyphColor.color);
GetComponent<MeshRenderer>().material = GlyphManager.glyphManager.BlockMaterials.Single(t => t.name == GlyphManager.glyphManager.SelectedGlyphColor.name);
}
}