Home > database >  How to understand" this.transform.position = target.TransformPoint(_camOffset)"?
How to understand" this.transform.position = target.TransformPoint(_camOffset)"?

Time:10-16

In unity

The gameobject doesn't have parent, which means the TransformPoint is not necessary, am I right for saying that?(worldPosition and localPosition should be the same) Is this code essentially is "Vector3 = Vector3"? seems that the TransformPoint() just change the Transform to Vector and offset the value of it with the parameter. There is no bug, but I really want to understand!!THX!!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraBehaviour : MonoBehaviour
{
    public Vector3 _camOffset = new Vector3 (0.41f,0.84f,-26.4f);
    private Transform target;
    void Start()
    {
        target = GameObject.Find("Hero 1").transform;
    }
    void LateUpdate()
    {
        this.transform.position = target.TransformPoint(_camOffset);
        this.transform.LookAt(target);
    }
}

CodePudding user response:

There's a few things going on, let me try to explain. The most important 'elephant in the room' is obviously the function of the Transform itself. It's pretty loaded in Unty3D, we'll break it down into three main parts: the data structure itself, its role in the rendering engine, and its methods.

Understanding transform as a data structure is trivial. Just think of it as a class consisting of just four fields: LocalPosition (Vector3), LocalRotation (Quaternion), Scale (Vector3) and a Parent (another Transform or null) reference. There might be more internally but you only ever worry about those four things, ever. Only the local positions and rotations are stored in the transform itself, absolute values need to be calculated taking into account the chain of parents, unless the parent is null than local=absolute.

Those fields are used by the rendering engine to position, scale and rotate the object verticies before sending a frame to rendering. The process is called 'transforming' which might seem confusing but is kind of important in your question case. when rendering objects that have non-null parent, the transformations are chained, so for example to obtain absolute world position of a gameobject that is not rotated and not scaled, it needs to add positions of all the parents up the chain until there is no parent.

The third element are the methods, which the Transform class contains quite a lot of. One of them is LookAt(Transform) which finds and applies rotation value, that makes ite transforms Forward vector point toward position of the target.

Now the main bit: TransformPoint is part of a suite of six methods (others are TransformVector, TransformDirection, InverseTransformPoint, InverseTransformVector, InverseTransformDirection). On a side note - for me personally it's often not very clear initially which one of those six I need, it takes a bit of experimenting often.

In your case, in the decription of TransformPoint in the manual we read:

"Transforms position from local space to world space."

This means that it takes input of a point expressed in the 'local' reference frame (as you were 'sitting' on the transform), but outputs the same point after doing all the 'transformations' (walking up the parent tree and multiplying rotations and scales, and applying offsets) to get to another Vector3 representing a point, this time not relative to the transform but relative to the world, the result is applied as world space position of the current (this) transform placing it behind it, respecting its rotation.

Is worth nothing that what actually happens in a parented situation is unity calculation local parameters so that the chain results in a desired absolute values.

To sum things up: Your _camOffset vector is expressed in relation to the 'target' transform, and as the 'target' transform moves and rotates, the result of TransformPoint will move and rotate as well, keeping the same offset and keeping the rotation (just adding the offset would ignore rotation). The result of this point transformation is then applied as world position of the current object (presumably a camera).

Then in the second line the camera, which has been placed behind the tracked object, is oriented to look at the object.

  • Related