Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help with inverse rotation (Solved)

Discussion in 'Scripting' started by spryx, Mar 5, 2017.

  1. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    (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:
    Code (CSharp):
    1. using System.Linq;
    2. using UnityEngine;
    3. using DG.Tweening;
    4.  
    5. public class PoolGlyphController : MonoBehaviour {
    6.  
    7.     public const float ScaleTime = 0.1f;
    8.     public const float RotationTime = 1f;
    9.     public const float ScaleBeforeRotation = 0.8f;
    10.  
    11.     [HideInInspector]
    12.     public GameObject RenderGlyph;
    13.  
    14.     private bool _canRotate = true;
    15.     private bool _facingBackwards;
    16.  
    17.     private static readonly Vector3[] RotVects =
    18.     {
    19.         new Vector3(180f,0f,0f),
    20.         new Vector3(-180f,0f,0f),
    21.         new Vector3(0f,180f,0f),
    22.         new Vector3(0f,-180f,0f),
    23.         new Vector3(0f,-180f,-90f),
    24.         new Vector3(0f,180f,-90f),
    25.         new Vector3(180f,0f,-90f),
    26.         new Vector3(-180f,0f,-90f)
    27.     };
    28.  
    29.     private Vector3 _returnVect;
    30.  
    31.     void OnMouseDown()
    32.     {
    33.         LimitedRotation(0f);
    34.     }
    35.  
    36.     public void LimitedRotation(float tdelay) {
    37.         if (_canRotate) {
    38.             if (!_facingBackwards) {
    39.                 DoRotate(RotVects[Random.Range(0, RotVects.Length)],tdelay);
    40.                 _facingBackwards = true;
    41.             }
    42.             else {
    43.                 GlyphManager.glyphManager.SelectRandomColor();
    44.                 GlyphManager.glyphManager.SelectRandomGlyph();
    45.                 SetGlyph();
    46.                 DoRotate(_returnVect,0f);
    47.                 _facingBackwards = false;
    48.             }
    49.         }
    50.     }
    51.  
    52.     void DoRotate(Vector3 rotationVect, float rDelay) {
    53.         if (!_canRotate)
    54.             return;
    55.         _canRotate = false;
    56.  
    57.         Sequence blockRotate = DOTween.Sequence().Append(
    58.         transform.DOScale(new Vector3(ScaleBeforeRotation, ScaleBeforeRotation, ScaleBeforeRotation), ScaleTime).SetDelay(rDelay).SetEase(Ease.InCirc));
    59.         blockRotate.Append(
    60.         transform.DOLocalRotate(rotationVect, RotationTime, RotateMode.LocalAxisAdd));
    61.         blockRotate.Append(
    62.         transform.DOScale(new Vector3(1f, 1f, 1f), ScaleTime).SetEase(Ease.InCirc).OnComplete(() => { _canRotate = true; _returnVect = -rotationVect; }));
    63.     }
    64.  
    65.     void SetGlyph()
    66.     {
    67.         if (RenderGlyph != null) {
    68.             RenderGlyph.GetComponent<MeshFilter>().sharedMesh = GlyphManager.glyphManager.SelectedGlyph.GetComponent<MeshFilter>().sharedMesh;
    69.             RenderGlyph.GetComponent<MeshRenderer>().material.SetColor("_Color", GlyphManager.glyphManager.SelectedGlyphColor.color);
    70.             GetComponent<MeshRenderer>().material = GlyphManager.glyphManager.BlockMaterials.Single(t => t.name == GlyphManager.glyphManager.SelectedGlyphColor.name);
    71.             return;
    72.         }
    73.  
    74.         RenderGlyph = Instantiate(GlyphManager.glyphManager.SelectedGlyph, transform);
    75.         RenderGlyph.transform.localPosition = new Vector3(0f, 0f, 1f);
    76.         RenderGlyph.transform.localRotation = transform.localRotation;//Quaternion.Euler(-180f, 0f, 0f);
    77.         RenderGlyph.GetComponent<MeshRenderer>().material.SetColor("_Color", GlyphManager.glyphManager.SelectedGlyphColor.color);
    78.  
    79.         GetComponent<MeshRenderer>().material = GlyphManager.glyphManager.BlockMaterials.Single(t => t.name == GlyphManager.glyphManager.SelectedGlyphColor.name);
    80.     }
    81.  
    82. }
    83.  
    Fixed Script:
    Code (CSharp):
    1. using System.Linq;
    2. using UnityEngine;
    3. using DG.Tweening;
    4.  
    5. public class PoolGlyphController : MonoBehaviour {
    6.  
    7.     public const float ScaleTime = 0.1f;
    8.     public const float RotationTime = 1f;
    9.     public const float ScaleBeforeRotation = 0.8f;
    10.  
    11.     [HideInInspector]
    12.     public GameObject RenderGlyph;
    13.  
    14.     private bool _canRotate = true;
    15.     private bool _facingBackwards;
    16.  
    17.     private static readonly Vector3[] RotVects =
    18.     {
    19.         new Vector3(180f,0f,0f),
    20.         new Vector3(-180f,0f,0f),
    21.         new Vector3(0f,180f,0f),
    22.         new Vector3(0f,-180f,0f),
    23.         new Vector3(0f,-180f,-180f),
    24.         new Vector3(0f,180f,-180f),
    25.         new Vector3(180f,0f,-180f),
    26.         new Vector3(-180f,0f,-180f)
    27.     };
    28.  
    29.     private Vector3 _returnVect;
    30.  
    31.     void OnMouseDown() {
    32.         LimitedRotation(0f);
    33.     }
    34.  
    35.     public void LimitedRotation(float tdelay) {
    36.         if (_canRotate) {
    37.             if (!_facingBackwards) {
    38.                 DoRotate(RotVects[Random.Range(0, RotVects.Length)], tdelay);
    39.                 _facingBackwards = true;
    40.             }
    41.             else {
    42.                 GlyphManager.glyphManager.SelectRandomColor();
    43.                 GlyphManager.glyphManager.SelectRandomGlyph();
    44.                 SetGlyph();
    45.                 DoRotate(_returnVect, 0f);
    46.                 _facingBackwards = false;
    47.             }
    48.         }
    49.     }
    50.  
    51.     void DoRotate(Vector3 rotationVect, float rDelay) {
    52.         if (!_canRotate)
    53.             return;
    54.         _canRotate = false;
    55.  
    56.         Sequence blockRotate = DOTween.Sequence().Append(
    57.         transform.DOScale(new Vector3(ScaleBeforeRotation, ScaleBeforeRotation, ScaleBeforeRotation), ScaleTime).SetDelay(rDelay).SetEase(Ease.InCirc));
    58.         blockRotate.Append(
    59.         transform.DOLocalRotate(rotationVect, RotationTime, RotateMode.LocalAxisAdd));
    60.         blockRotate.Append(
    61.         transform.DOScale(new Vector3(1f, 1f, 1f), ScaleTime).SetEase(Ease.InCirc).OnComplete(() =>
    62.         {
    63.             _canRotate = true;
    64.             _returnVect = -rotationVect;
    65.         }));
    66.     }
    67.  
    68.     void SetGlyph() {
    69.         if (RenderGlyph != null) {
    70.             RenderGlyph.GetComponent<MeshFilter>().sharedMesh = GlyphManager.glyphManager.SelectedGlyph.GetComponent<MeshFilter>().sharedMesh;
    71.             RenderGlyph.GetComponent<MeshRenderer>().material.SetColor("_Color", GlyphManager.glyphManager.SelectedGlyphColor.color);
    72.             GetComponent<MeshRenderer>().material = GlyphManager.glyphManager.BlockMaterials.Single(t => t.name == GlyphManager.glyphManager.SelectedGlyphColor.name);
    73.             return;
    74.         }
    75.  
    76.         RenderGlyph = Instantiate(GlyphManager.glyphManager.SelectedGlyph, transform);
    77.         RenderGlyph.transform.localPosition = new Vector3(0f, 0f, 1f);
    78.         RenderGlyph.transform.rotation = Quaternion.Euler(_returnVect);
    79.         RenderGlyph.GetComponent<MeshRenderer>().material.SetColor("_Color", GlyphManager.glyphManager.SelectedGlyphColor.color);
    80.  
    81.         GetComponent<MeshRenderer>().material = GlyphManager.glyphManager.BlockMaterials.Single(t => t.name == GlyphManager.glyphManager.SelectedGlyphColor.name);
    82.     }
    83.  
    84. }
    85.  
     
    Last edited: Mar 5, 2017
  2. booiljoung

    booiljoung

    Joined:
    May 12, 2013
    Posts:
    57
    Hi,

    _returnVect는 private이다.
    _returnVect 는 어느 코드에 초기화 되느냐?

    if you have more question mail me : booiljung@me.com.
     
  3. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    Edit: I have fixed the problem and updated my post with the fixed script. Hope this helps someone else. Seems I had the vectors wrong.