简体   繁体   中英

how do i get c# code to print position and rotation values from inspector to console?

i am a beginner at unity and C# and struggling to get the position nad rotation valuse form the inspector to the console i am a beginner at unity and C# and struggling to get the position nad rotation valuse form the inspector to the console

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

public class positions : MonoBehaviour
{
    float rx;
    float ry;
    float rz;
    float tx;
    float ty;
    float tz;
    // Start is called before the first frame update
    void Start()
    {
           Debug.Log(rx);
           Debug.Log(ry);
           Debug.Log(rz);
           Debug.Log(tx);
           Debug.Log(ty);
           Debug.Log(tz);
        
        print(rx);



    }

    // Update is called once per frame
    void Update()
    {
        rx = GetComponent<Transform>().rotation.x; 
        ry = transform.localEulerAngles.y;
        rz = transform.localEulerAngles.z;

        tx = transform.position.x;
        ty = transform.position.y;
        tz = transform.position.z;

    }
}

I assume you mean update the console every frame?

If that is the case, then you would need to do this in the Update() method.

The Start() method is only called once, just before the first frame is updated.

The Update() method is called once per frame.

You can see this in the auto comment above each method in your code.

void Update()
{
    rx = GetComponent<Transform>().rotation.x; 
    ry = transform.localEulerAngles.y;
    rz = transform.localEulerAngles.z;

    tx = transform.position.x;
    ty = transform.position.y;
    tz = transform.position.z;

    Debug.Log(rx);
    Debug.Log(ry);
    Debug.Log(rz);
    Debug.Log(tx);
    Debug.Log(ty);
    Debug.Log(tz);
}

The above code would print the values to the console once for every frame.

An example can be seen in the following screenshot:

在此处输入图像描述

You can also see in the bottom left hand corner that the most recent print statement is displayed.

Hope this helps.

You can get the inspector's rotations using TransformUtils.GetInspectorRotation and using transform.position to get the position as Vector3

Vector3 rv;
Vector3 p;

void Update()
{
rv = UnityEditor.TransformUtils.GetInspectorRotation(transform);
Debug.Log("Rotations: "+ rv.x +" "+rv.y+" "+rv.z);
p  = transform.position;
Debug.Log("Positions: "+ p.x +" "+p.y+" "+p.z);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM