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

Issue with transform

Discussion in 'Scripting' started by emirakyuz81, Aug 22, 2022.

  1. emirakyuz81

    emirakyuz81

    Joined:
    Aug 9, 2022
    Posts:
    4
    Hi there, newbie on unity trying to make Bridge Race to learn stacking mechanics. I've just done stacking up into character & stair down but there is an issue with stair stacking, it's rotation changes with where my player looks even I give localRotation of referenceStair which is already in scene before game start. Here is my project's ss and codes I hope someone can give me a way to fix this.

    Code (CSharp):
    1.  
    2.     public static CollectManager instance;
    3.     public Transform refStair;
    4.     private Stack<GameObject> collectedStack = new Stack<GameObject>();
    5.     [SerializeField] private GameObject poppedStack;
    6.     [SerializeField] private float distanceObjects;
    7.     [SerializeField] private Transform prevObject, lastStack;
    8.     [SerializeField] private Transform parent;
    9.     int counter = 0, counter2 = 0;
    10.  
    11.    
    12.     private void Awake()
    13.     {
    14.         if (instance == null)
    15.         {
    16.             instance = this;
    17.         }
    18.     }
    19.     void Start()
    20.     {
    21.         distanceObjects = prevObject.localScale.y;
    22.         lastStack.localPosition = refStair.localPosition;
    23.     }
    24.  
    25.    
    26.     void Update()
    27.     {
    28.  
    29.     }
    30.  
    31.     public void CollectUp(GameObject collectedObject, bool needTag = false, string tag = null, bool downOrUp = true)
    32.     {
    33.         collectedStack.Push(collectedObject);
    34.         Debug.Log(collectedStack.Count);
    35.         counter++;
    36.         if (needTag)
    37.         {
    38.             collectedObject.tag = tag;
    39.         }
    40.         collectedObject.transform.parent = parent;
    41.         Vector3 desPos = prevObject.localPosition;
    42.         //desPos.y += downOrUp ? distanceObjects : -distanceObjects;
    43.         desPos.y = distanceObjects * counter;
    44.         collectedObject.transform.DOLocalJump(desPos, 0.4f, 1, 0.2f); //collectedObject.transform.localPosition = desPos;
    45.         collectedObject.transform.localRotation = prevObject.localRotation;
    46.         //prevObject = collectedObject.transform;
    47.     }
    48.  
    49.     public void StairManage(string tag = null, bool downOrUp = true)
    50.     {
    51.         if (collectedStack.Count != 0)
    52.         {
    53.             counter--;
    54.             counter2++;
    55.             Vector3 desPos2 = lastStack.transform.localPosition;
    56.             poppedStack = collectedStack.Pop();
    57.             poppedStack.tag = tag;
    58.             poppedStack.transform.localScale = refStair.localScale;
    59.             desPos2.y = refStair.transform.localScale.y * counter2;
    60.             desPos2.z = refStair.transform.localScale.z * counter2;
    61.             poppedStack.transform.DOLocalJump(desPos2, 0.2f, 1, 0.2f);
    62.             poppedStack.transform.localRotation = refStair.transform.localRotation;
    63.             poppedStack.transform.parent = null;
    64.             lastStack = poppedStack.transform;
    65.         }
    66.     }
    upload_2022-8-22_11-46-56.png

    Player's collect manager script
    upload_2022-8-22_11-47-45.png

    upload_2022-8-22_11-50-17.png

    This transform side of Unity is really confusing me too much.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    Had to google what 'Bridge Race' was. Shows how little I play mobile games. Which is exactly zero.

    I wouldn't even bother coding the functionality to place blocks. Just have them already placed and marked as inactive.

    Then if the player is holding one as they walk into a trigger, you just destroy one off their back and make the next stair active.
     
  3. emirakyuz81

    emirakyuz81

    Joined:
    Aug 9, 2022
    Posts:
    4
    Yeah I was thinking about same way to make stairs but then I wouldn't want to hold many objects for it, decided to make it in this way, I don't wanna do it by just watching video without adding my opinions in it. Just couldn't figure it out why rotation changes with player's direction. Still thanks for your solution idea.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    You're over complicating what should be a simple thing. Sometimes the solution isn't to solve what you're trying to do, but find a better way to do it.

    There's no point in overcomplicating something just to be different to what already exists.
     
  5. emirakyuz81

    emirakyuz81

    Joined:
    Aug 9, 2022
    Posts:
    4
    I understand but there will be different levels and those many objects won't be good for optimization, there will be 11x stair area so that's mean too many hidden object in project that's why I choose this way.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    This is pre mature optimisation. There are much more worthwhile optimisations to worry about than a few hundred inactive game objects, which, by the way, will hardly be an issue.
     
  7. emirakyuz81

    emirakyuz81

    Joined:
    Aug 9, 2022
    Posts:
    4
    Just fixed with changing rotation code with eulerAngles now it working fine