Search Unity

Detecting if a gameobject is not standing up.

Discussion in 'Scripting' started by FullMe7alJacke7, Feb 14, 2017.

  1. FullMe7alJacke7

    FullMe7alJacke7

    Joined:
    Dec 17, 2013
    Posts:
    55
    Hey everyone, I'm trying to check to see if an object is standing up. for some reason when I imported my model from blender and I put it into the game, the roation has to be -90 in the X direction in Unit for the object to be "Standing up" so I figured okay cool no problem, make a child object and just set its rotation to 0 across the board and get the localroation of that. However it is still getting the transform of the parent and returning the 270 degrees when its standing up. Is there a way I can code around this? I tried to find a fix for it a different way and have come up empty handed. Heres the code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pins : MonoBehaviour {
    6.     public float standingThreshold = 3f;
    7.     // Use this for initialization
    8.     void Start () {
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.         print(name + IsStanding());
    14.     }
    15.  
    16.     public bool IsStanding()
    17.     {
    18.         Transform tiltSensor = GetComponentInChildren<Transform>();
    19.         Vector3 RoatationInEuler = tiltSensor.localRotation.eulerAngles;
    20.  
    21.         float TiltInX = Mathf.Abs(RoatationInEuler.x);
    22.         float TiltinZ = Mathf.Abs(RoatationInEuler.z);
    23.         print(TiltInX + "" + TiltinZ);
    24.  
    25.         if (TiltInX < standingThreshold && TiltinZ < standingThreshold)
    26.         {
    27.             return true;
    28.         }
    29.         else
    30.         {
    31.             return false;
    32.         }
    33.     }
    34. }
    35.  
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You could just load it up in blender, rotate it in there remembering that ion blender the Z axis is up.

    Once you have it facing up on the Y axis, press Ctrl + A in object mode and select rotation.

    Export it again and all will be well.
     
    FullMe7alJacke7 likes this.
  3. FullMe7alJacke7

    FullMe7alJacke7

    Joined:
    Dec 17, 2013
    Posts:
    55
  4. Jaxom

    Jaxom

    Joined:
    Feb 24, 2016
    Posts:
    5
    In blender using the FBX exporter options there should be a tick box for set y = up. I recommend using the FBX and not letting unity just read form the .blend.
     
    FullMe7alJacke7 likes this.
  5. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    It's not standing up. Look in the corner, Z axis is up in blender, Y axis is up in Unity.

    If you are stuck, feel free to pm me a link with the models and I'll fix them for you.
     
    FullMe7alJacke7 likes this.
  6. FullMe7alJacke7

    FullMe7alJacke7

    Joined:
    Dec 17, 2013
    Posts:
    55
    Hey guys I appreciate your help! Even though this happened to be more of a blender fix than the code. I did as Jaxom recommended and used the FBX export options to change the way the model was exported and all is well!

    I must've rotated it incorrectly when I tried to do it manually in blender the first time. Either way, code works flawlessly now and I have no weird rotation to make my objects stand up. Thanks!

    I guess the way I was thinking about the exporting last night, I figured if it was upwards in blender, it would translate that to being upright in unity.