UnityAPI学习之Transform组件基本使用

目录

Transform组件

访问与获取

Transform的位置和旋转信息

Transform局部坐标和旋转信息的获取

Transform的缩放与正方向

缩放(Scale)

正方向

Transform相关的查找方法

销毁游戏物体


Transform组件

访问与获取

现在创建一个容器放置GrisGO物体的、Transform组件并输出其名字

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

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);
            
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Transform的位置和旋转信息

示例1:获取GrisGO的位置信息和旋转信息

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

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

示例2:

在以上实例中,获取的旋转信息是以四元数的形式表示出来(如在rotation属性中,x=1.58,但实际输出的四元数为0.01379),现在要将rotation属性以其原本的数值(欧拉角/度)形式展现出来

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

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);
        Debug.Log("GrisGO以欧拉角/度的形式表示旋转结果为" + GrisTrans.eulerAngles);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Transform局部坐标和旋转信息的获取

局部坐标:子级相对于父级的位置信息

示例:输出GrisGO的局部坐标和旋转欧拉角

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

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO局部坐标为" + GrisTrans.localPosition);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);
        Debug.Log("GrisGO以欧拉角/度的形式表示旋转结果为" + GrisTrans.eulerAngles);
        Debug.Log("GrisGO的局部欧拉角/度的形式表示旋转结果为" + GrisTrans.localEulerAngles);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Transform的缩放与正方向

缩放(Scale)

对于Scale属性,只有局部缩放LocalScale

示例:

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

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        /*Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO局部坐标为" + GrisTrans.localPosition);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);
        Debug.Log("GrisGO以欧拉角/度的形式表示旋转结果为" + GrisTrans.eulerAngles);
        Debug.Log("GrisGO的局部欧拉角/度的形式表示旋转结果为" + GrisTrans.localEulerAngles);*/
        Debug.Log("GrisGO的局部缩放为" + GrisTrans.localScale);


    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

正方向

正方向即关于xyz轴所指的方向,其中分为有相对于世界空间的正方向和本地正方向(若无父级,则本地正方向与世界正方向相同),根据实际开发中的需求不同,调整相应的世界空间或者本地正方向的数值大小

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

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        /*Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO局部坐标为" + GrisTrans.localPosition);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);
        Debug.Log("GrisGO以欧拉角/度的形式表示旋转结果为" + GrisTrans.eulerAngles);
        Debug.Log("GrisGO的局部欧拉角/度的形式表示旋转结果为" + GrisTrans.localEulerAngles);*/
        ///Debug.Log("GrisGO的局部缩放为" + GrisTrans.localScale);
        Debug.Log("GrisGO关于x轴的正方向为" + GrisTrans.right);
        Debug.Log("GrisGO关于y轴的正方向为" + GrisTrans.up);
        Debug.Log("GrisGO关于z轴的正方向为" + GrisTrans.forward);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Transform相关的查找方法

示例:

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

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        /*Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO局部坐标为" + GrisTrans.localPosition);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);
        Debug.Log("GrisGO以欧拉角/度的形式表示旋转结果为" + GrisTrans.eulerAngles);
        Debug.Log("GrisGO的局部欧拉角/度的形式表示旋转结果为" + GrisTrans.localEulerAngles);*/
        ///Debug.Log("GrisGO的局部缩放为" + GrisTrans.localScale);
        Debug.Log("GrisGO关于x轴的正方向为" + GrisTrans.right);
        Debug.Log("GrisGO关于y轴的正方向为" + GrisTrans.up);
        Debug.Log("GrisGO关于z轴的正方向为" + GrisTrans.forward);


        //查找
        Debug.Log("当前脚本挂载的游戏对象下的叫Gris的子对象身上的Transform组件是" + transform.Find("Gris"));
        //索引
        Debug.Log("当前脚本挂载的游戏对象下的叫Gris的子对象身上的Transform组件是" + transform.GetChild(0));
        Debug.Log("Gris当前在此父对象同级里所在的索引位置" + GrisTrans.GetSiblingIndex());
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

销毁游戏物体

Transform.Destroy(GrisGO);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/673782.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

VueX核心内容

聚沙成塔每天进步一点点 本文内容 ⭐ 专栏简介Vuex 核心内容核心概念1. State(状态)示例: 2. Getter(获取器)示例: 3. Mutation(突变)示例: 4. Action(动作&a…

MbedTLS源码跨平台编译(window/macos/linux)

1.window平台编译: 克隆: git clone --recursive https://github.com/Mbed-TLS/mbedtls.git 克隆成功 添加OpenSSL环境变量 验证环境 使用cmake编译 cmake ../生成配置时出错 出现上面原因是克隆下来的library与programs及tests目录少文件了,直接下载zip包替换library目录

dibbler-DHCPv6 的开源框架(C++ 实现)1

一、下载 IPv6 DHCPv6 协议的开源框架:dibbler 下载地址:https://github.com/tomaszmrugalski/dibbler.git 二、代码编写语言和文件结构 编写语言 文件 三、编译 编译 server 端: chmod x configure ./configure# 编译服务端(4核) mak…

Renesas MCU之使用e² studio搭建开发环境

目录 概述 1 e studio介绍 2 搭建Renesas MUC开发环境 2.1 软件版本信息 2.2 安装软件 3 创建工程 3.1 板卡硬件接口 3.2 FSP配置IO 4 Generate Project 4.1 项目目录介绍 4.2 LED接口相关驱动 5 调试 5.1 测试代码 5.2 J-Link调试代码 5.3 硬件结构 概述 本文主…

【ARM】

ARM ■ 指令集■ 1. RISC■ 2. CISC ■ ARM简介■ 1.■ 2. ■ ARM-CPU体系架构■ 1. M0■ 2. M3■ 3. M4■ 4. M7■ 5. M7■ 6. M7 ■ ARM-寄存器■ 1. 通用寄存器■ 2.■ 3.■ 4. ■ ARM-工作模式■ ARM-寄存器组■ ARM-异常向量表■ 由于soc0x00000000 是存放IROM芯片出厂数据…

由MapTile引发的ResultSet的思考及实践

其实这篇文章应该是上周末来写的,但是苦逼啊。别人都抱怨工作996,我特么直接9117了,连轴转12天,完全没有个人时间,苦逼啊! 本来周末计划看完龙珠Z(日语)布欧篇 呢,给自己…

喵星人必备!福派斯三文鱼猫粮,营养满分!

猫粮品牌:福派斯三文鱼猫粮测评体验 在快节奏的都市生活中,我们的宠物猫也需要适应当下的生活环境,并保持健康和活力。作为一名合格的铲屎官,我们总是关心如何为猫咪提供既健康又美味的饮食。今天,我有幸为大家带来一…

Gradle如何发布一个Android开源框架到JitPack

序言 在Android领域耕耘了多年的老司机们,技术大多已经沉淀到足以自己写各种各样的框架了。你有没有想过?其实你可以将写好的框架开源出来,让更多人受益,提升开发效率。亦或者是引用过别人com.github打头的开源框架,但是不知道自…

(一)django目录介绍

1、生成django项目,得到的目录如下 manage.py:命令行工具,内置多种方式与项目进行交互。在命令提示符窗口下,将路径切换到项目并输入python manage.py help,可以查看该工具的指令信息。 默认的数据库工具,sqlite 在…

SQL注入-时间盲注

SQL时间盲注(Time-based Blind SQL Injection),又叫延时注入,是一种SQL注入攻击技术,用于在无法直接获取查询结果或查看响应内容变化的情况下,通过引入时间延迟来推断数据库的信息;时间盲注依赖…

UE 熟悉引擎

项目文件 模式: 参考视频:【基础】01课:创建项目全流程和模板试玩_哔哩哔哩_bilibili

YOLOv1深入解析与实战:目标检测算法原理

参考: https://zhuanlan.zhihu.com/p/667046384 https://blog.csdn.net/weixin_41424926/article/details/105383064 https://arxiv.org/pdf/1506.02640 1. 算法介绍 学习目标检测算法,yolov1是必看内容,不同于生成模型,没有特别…

计算机毕业设计 | 基于Koa+vue的高校宿舍管理系统宿舍可视化系统

项目介绍 项目背景 随着科技的发展,智能化管理越来越重要。大学生在宿舍的时间超过了1/3,因此良好的宿舍管理对学生的生活和学习极为关键。学生宿舍管理系统能够合理安排新生分配宿舍,不浪费公共资源,减轻学校管理压力&#xff…

[极速版]写个linux探测自己机器ip地址的tool(基于shell + sshpass)

背景:那个房间没有能正常上广域网的网口,就用了个无线中继 适用情况:上级路由ssh or teamviewer访问下级路由的机器,但下级路由不支持查看IP 自行完成下级路由(此处指无线中继)的端口映射or DMZ整机映射 a…

Codeforces Round 949 D. Turtle and Multiplication 【欧拉路径】

题意 要求构造一个长度为 n n n 的序列 a a a&#xff0c;使得&#xff1a; ∀ i ∈ [ 1 , n ] , 1 ≤ a i ≤ 3 ⋅ 1 0 5 \forall i \in [1,n], \; 1 \leq a_i \leq 3 \cdot 10^5 ∀i∈[1,n],1≤ai​≤3⋅105 ∀ 1 ≤ i < j ≤ n − 1 , a i ⋅ a i 1 ≠ a j ⋅ a j 1…

Java筑基-面向对象

Java-面向对象 一、类和对象1、类和对象的关系2、创建类3、创建对象4、成员变量与局部变量5、构造器5.1、创建对象的过程5.2、构造器的格式5.3、构造器和方法的区别5.4、构造器的作用5.5、构造器的重载 6、this关键字用法&#xff1a;6.1、this可以修饰属性6.2、this可以修饰方…

每日一题——Python实现PAT甲级1046 Shortest Distance(举一反三+思想解读+逐步优化)

一个认为一切根源都是“自己不够强”的INTJ 个人主页&#xff1a;用哲学编程-CSDN博客专栏&#xff1a;每日一题——举一反三Python编程学习Python内置函数 Python-3.12.0文档解读 目录 我的写法 专业点评 优点 改进建议 时间复杂度分析 空间复杂度分析 总结 我要更…

第一篇【传奇开心果系列】AI工业应用经典算法和Python示例:基于AI的智能制造技术经典算法与Python实践

传奇开心果博文系列 系列博文目录AI工业应用经典算法和Python示例系列 博文目录前言一、AI在智能制造方面的应用场景介绍二、基于AI的智能制造技术经典算法介绍三、支持向量机机器学习算法Python示例代码四、随机森林机器学习算法Python示例代码五、深度学习算法Python示例代码…

HTML5常用标签表单from

form表单标签 <!-- form表单其实就是一种&#xff1a;客户端和服务端数据交流一种方式机制。1&#xff1a; 服务端&#xff0c;提供数据接受地址&#xff08;gin/beego/inris&#xff09;比如&#xff1a;http://localhost:8080/toLogin2: 因为浏览器&#xff0c;在提交数据…

sql server数据库连接不上

我遇到了一个问题&#xff0c;本地sql server怎么都连接不了 我按照网上的方法都试了一遍&#xff0c;发现都错了 后来我把tcp/ip禁用了就好了 或者说把tcp/ip改成动态端口 之后需要重启sql server&#xff0c;右键选中的地方&#xff0c;重启