Search Unity

How do you apply a rotation in unity?

Discussion in 'Editor & General Support' started by beez-dev, Sep 7, 2021.

  1. beez-dev

    beez-dev

    Joined:
    Aug 13, 2021
    Posts:
    47
    The apply word above comes from 'blender 3D'. Basically, once you rotate an object , it's x, y, z value changes. What apply does is keep the transformation, but, change the x, y, z value back to 0.

    Is this possible in unity? How do I do this?
     
  2. CONGOBILL

    CONGOBILL

    Joined:
    Mar 4, 2020
    Posts:
    17
    I don't know if I understood the question. I think what you mean is to apply a rotation in angles, instead of adding one. To do that you can save the gameObject angles, and set a new Rotation Vector3.

    Code (CSharp):
    1. var rot = gameObject.transform.localRotation.eulerAngles; //get the angles
    2. rot.Set(0f, 90f, 0f); //set the angles
    3. gameObject.transform.localRotation = Quaternion.Euler(rot); //update the transform
     
  3. beez-dev

    beez-dev

    Joined:
    Aug 13, 2021
    Posts:
    47

    Hey there, thanks for the answer, but, this code seems to be actually rotating the object(is it?) What I was trying to do seems to be rather impossible from unity(unless there's an api to access mesh?). I've elaborated my issue here as well: https://gamedev.stackexchange.com/questions/196678/how-do-you-apply-a-rotation-in-unity
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @beez-dev if you want to apply your rotation to a mesh, you have to modify it. Instead of rotating the Transform of the object you would rotate the mesh data. Then the mesh could rotate 45 degrees to the right (for example) visually, but the rotation would still be zero. But then you would have to save the mesh somehow.

    What are you exactly trying to achieve? This kind of things are better done outside Unity if your asset is not correct.
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Unity has a full API for access to the mesh so you can make any changes you want or even create meshes from scratch. Unless you need to do this at runtime, though, it would be much easier to just do this in Blender or Probuilder.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    Totally possible, doing what @Olmi and / or @kdgalla hint at above.

    Here's a super-simple script that applies a scale factor to a mesh, letting me maintain (1,1,1) scaling at the GameObject.Transform level:

    https://gist.github.com/kurtdekker/c430a870f344117b8f3b47f11aa0a13f

    I just copied that out of my Jetpack Kurt game. It would be trivial to also make it accept a Rotation, and then multiply each mesh by the rotation, not just the scale.
     
  7. beez-dev

    beez-dev

    Joined:
    Aug 13, 2021
    Posts:
    47
    actually , this is my problem: https://imgur.com/Mi1MzE3 , the reason I was trying to apply my rotation was because, the local axes of the player rig and the camera(it's children) were in different direction. and once I select the player rig(the parent of the camera) > Align to view, then, the camera winds up in a different direction. Please let me know a solution if you view the video above.