Search Unity

HELP! Object rotates, but does not rotate after all!

Discussion in 'Scripting' started by LoekvanKooten, Feb 20, 2019.

  1. LoekvanKooten

    LoekvanKooten

    Joined:
    Apr 3, 2017
    Posts:
    120
    I'm obviously missing something very basic here. This gameobject has a lid with childed elements. The lid including the childed elements is rotated, as you can see. However, the childed elements do not rotate with it. However, if I rotate the lid from the Inspector using exactly the same values, the entire lid rotates. It probably has to do with the way the prefab is built, but I have no idea what is going on here. What am I missing?



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Chest : MonoBehaviour
    6. {
    7.     public GameObject chestLid;
    8.     Quaternion closedLidPosition;
    9.     Quaternion openLidPosition = Quaternion.Euler(new Vector3(48.94f, 0, 0));
    10.     public bool openMe = false;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         closedLidPosition = chestLid.transform.localRotation;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (openMe == true)
    22.         {
    23.             openMe = false;
    24.             OpenChest();
    25.         }
    26.     }
    27.  
    28.     public void OpenChest()
    29.     {
    30.         StartCoroutine(CoOpenChest(chestLid, 4f));
    31.     }
    32.  
    33.     IEnumerator CoOpenChest(GameObject chestLid, float openTime)
    34.     {
    35.         float t = 0;
    36.         while (t < openTime)
    37.         {
    38.             t += Time.deltaTime;
    39.             chestLid.transform.localRotation = Quaternion.Lerp(closedLidPosition, openLidPosition, (t / openTime));
    40.             yield return null;
    41.         }
    42.     }
    43. }
    44.  
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    431
    Meshes with animations should not be marked static. Look at the name of the mesh on your tomb objects. You'll see they are "Combine mesh (root scene)" which indicates that you have marked them static, and they're being batched by Unity.
     
    LoekvanKooten likes this.
  3. LoekvanKooten

    LoekvanKooten

    Joined:
    Apr 3, 2017
    Posts:
    120
    Argh, I completely overlooked that! Thank you so much :) Solved!