Table of Contents

Transform Position

You can find this sample in the tutorial project: MenuTransform Position

Explanation

This C# Beginner tutorial covers the Transform component of an entity.

The Transform component is such a commonly used component, that you can quick access it via Entity.Transform.

The transform contains all kinds of properties and methods for Position, Rotation and Scale. In this example we learn the difference between local and world position.

Transform Position

Code

using Stride.Core.Mathematics;
using Stride.Engine;

namespace CSharpBeginner.Code
{
    /// <summary>
    /// This script demonstrates how to access the entity's local and world position and displays them on screen. 
    /// <para>
    /// https://doc.stride3d.net/latest/en/tutorials/csharpbeginner/transform-position.html
    /// </para>
    /// </summary>
    public class TransformPositionDemo : SyncScript
    {
        public override void Start() { }

        public override void Update()
        {
            // We store the local and world position of our entity's tranform in a Vector3 variable
            Vector3 localPosition = Entity.Transform.Position;
            Vector3 worldPosition = Entity.Transform.WorldMatrix.TranslationVector;

            // We display the entity's name and its local and world position on screen
            DebugText.Print(Entity.Name + " - local position: " + localPosition, new Int2(400, 450));
            DebugText.Print(Entity.Name + " - world position: " + worldPosition, new Int2(400, 470));
        }
    }
}