using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class c4 : MonoBehaviour
{
void Start()
{
/*
// 获取位置
// 世界坐标系
Debug.Log(transform.position);
// 相对父物体相对坐标系
Debug.Log(transform.localPosition);
// 获取旋转
// 世界坐标系的旋转 类型:四元数
Debug.Log(transform.rotation);
// 相对父物体相对坐标系的旋转 类型:四元数
Debug.Log(transform.localRotation);
// 世界坐标系的旋转 类型:欧拉角
Debug.Log(transform.eulerAngles);
// 相对父物体相对坐标系的旋转 类型:欧拉角
Debug.Log(transform.localEulerAngles);
// 获取缩放
//
Debug.Log(transform.localScale);
// Z轴 蓝色 前方
// x轴 红色 右方
// y轴 绿色 上方
// 获取轴向 向量 (0,0,0)
// 前方
Debug.Log(transform.forward);
// 右方
Debug.Log(transform.right);
// 上方
Debug.Log(transform.up);
*/
// 父子关系 transform是指自己,可以换成其他的物体
// 获取父物体
GameObject f1 = transform.parent.gameObject;
Debug.Log(f1.name);
// 子物体个数
Debug.Log(transform.childCount);
// 获取子物体 参数:(物体名称)
Transform trans = transform.Find("Sphere");
trans = transform.GetChild(0);
// 解除子物体的父子关系
transform.DetachChildren();
// 判读一个物体是不是另一个物体的子物体
bool res = trans.IsChildOf(transform);
Debug.Log(res);
// 设置为父物体
trans.SetParent(transform);
}
void Update()
{
// 时时刻刻看向某点
// transform.LookAt(new Vector3(0, 0, 0));
// 持续旋转 参数(某轴、角度)
// transform.Rotate(Vector3.up, 1);
// 绕某个物体旋转 参数(点、某轴、速度)
// transform.RotateAround(new Vector3(0,0,0),Vector3.up,1);
// 移动 参数:(某轴、位移)
// transform.Translate(Vector3.forward * 0.1f);
//
}
}
Unity 坐标系组件(transform)位置获取、获取子物体、朝向、旋转、按某轴前进、
于 2024-03-02 15:00:45 首次发布
![](https://img-home.csdnimg.cn/images/20240611030827.png)