Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Rotate vertices and bones transforms while reading mesh data

Discussion in 'Scripting' started by Danil0v3s, Aug 11, 2023.

  1. Danil0v3s

    Danil0v3s

    Joined:
    Sep 22, 2014
    Posts:
    45
    Hi all, I'm working on a rework of a game which uses SMD file format. We so far are able to import everything without any problem, meshes, bones, bone weights and animation clips and everything else work fine.

    However when I load the mesh its forward is poiting down. I've been using this script to change the vertices in a way the mesh points forward after loaded, the problem now is the animations get completely borked.

    Code (CSharp):
    1.  
    2. private static Vector3 FixRotation(Vector3 v) {
    3.         var center = Vector3.zero;
    4.         var newRotation = new UnityEngine.Quaternion {
    5.             eulerAngles = new Vector3(-90, 90, 90)
    6.         };
    7.  
    8.         return newRotation * (v - center) + center;
    9. }
    10.  
    I have tried applying that same change to all the mesh bones and also rotation the animation clip quaternions but the animation after that gets even more creepier.

    I know I could just rotate the parent GameObject by -90 on the X and it would be fixed but I'm trying to fix this at the lowest level possible. What would be the correct approach to make the mesh look forward and have its animations working properly?
     
  2. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    78
    The fix at the lowest level possible would be to export the mesh/animations in such a way that it's oriented correctly. Basically every 3D modeling software has settings for that, since different engines often use different coordinate systems.
     
  3. Danil0v3s

    Danil0v3s

    Joined:
    Sep 22, 2014
    Posts:
    45
    Forgot to mention other than that. The lowest possible level within the editor boundaries xD
     
  4. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    78
    I know you said within the editor boundaries but what about importing the mesh into Blender and exporting it in the correct orientation?

    Otherwise you would have to apply the correct rotation to every single keyframe of your animations. The AnimationUtility class contains functions to edit keyframe data in animation clips, but I've never used that API before so sadly I can't really help you with that.
     
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Do you mean put it into a parent object and rotate said object(child) by 90?

    If so, that's what I would do, as I normally have the main parent as an empty object with player script and handle parent/childs appropriately. If the animations still work, then I wouldn't mess with them, just sounds like the object in question needs rotated.

    This would be the only other way, as far as I know, which sounds like "headache city" to me. :confused:

    But for sure, trying to manipulate forwards or root positions is a ton of work. Not to mention unnecessary large amounts of work. It's much easier to let background tasks, and local positions handle all that leg-work. :)
     
  6. Danil0v3s

    Danil0v3s

    Joined:
    Sep 22, 2014
    Posts:
    45
    Oh yea, I actually went the blender way with a simple-ish script to import the model, change the world up and export.. Took a couple of hours but it did the job brilliantly