docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Using transforms

    To use transforms in your project, use the Unity.Transforms namespace to control the position, rotation, and scale of any entity in your project.

    LocalTransform represents the relative position, rotation, and scale of the entity. If there is a parent, the transform is relative to that parent. If there is no parent, the transform is relative to the world origin. You can read and write to this component.

    public struct LocalTransform : IComponentData
    {
        public float3 Position;
        public float Scale;
        public quaternion Rotation;
    }
    

    Using the API

    There are no methods in the API to modify LocalTransform. All methods return a new value, and do not change the transform itself. So if you want to modify the transform, you must use the assignment operator. For example, to rotate a transform around the Z axis:

        myTransform = myTransform.RotateZ(someAngle);
    

    The only way to modify LocalTransform directly is by writing to the Position, Rotation, and Scale properties. For example:

        myTransform.Position += math.up();
    

    This code is equivalent to:

        myTransform = myTransform.Translate(math.up());
    

    There are several methods to construct a transform for you. So if you want to create a LocalTransform with a specified position, but using default rotation and scale, use this:

        var myTransform = LocalTransform.FromPosition(1, 2, 3);
    

    Using a hierarchy

    You can use LocalTransform by itself. However, if you want to use a hierarchy of Entities, you must also use Parent. To set a parent of a child entity use Parent:

    public struct Parent : IComponentData
    {
        public Entity Value;
    }
    

    To make sure that the parents find their children, and to set up their child component, ParentSystem must run.

    Use the static flag for everything that isn't going to move. This improves performance and reduces memory consumption.

    The transform system is optimized for large numbers of hierarchies at the root level. A root level transform is a transform with no parent. Avoid having large hierarchies under a single root. The work of concatenating hierarchical transforms is divided across jobs at the root level. So the worst case scenario is to keep large numbers of non-static entities under a single root.


    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Using the API
    • Using a hierarchy
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)