当前位置: 首页 > article >正文

Transform设置父物体,查找子物体+Input类

设置父物体

在代码中更深入理解GameObject和Transform 

public GameObject target;
public Transform target_transform;
transform.parent=target.transform;
transform.parent=target_transform;

transform.SetParent(target.transform);
transform.SetParent(target_transform);

因为GameObject肯定有Transform组件 ,所以这样做target.transform;

因为Transform不能独立存在,所以必然存活于游戏物体,所以可以这样做target_transform.gameObject;

transform查找子物体API

transform.childCount:子物体的数量。

transform.GetChild(i);根据index来查找子物体,特性:子物体的子物体是不会管的。

Transform t=transform.find("Cube(2)"); 根据游戏物体的名字进行精确查找,也不会搜索子物体的子物体

GameObject查找游戏物体

GameObject go=GameObject.Find("GameObject");能根据游戏物体名字在所有场景内游戏物体中查找,好用但是效率很低,谨慎使用;

通过Tag来进行查找游戏物体
        //当有多个tag是NPC的时候,查找到的是最后创建的tag是NPC的游戏物体
        //查找游戏物体tag是NPC
        //GameObject go=GameObject.FindGameObjectWithTag("NPC");
        //if(go)
        //  Debug.Log("go:"+go.name);

        //当游戏场景中有多个游戏物体tag是NPC,同时查找到,并返回一个游戏物体数组
        GameObject[] gos = GameObject.FindGameObjectsWithTag("NPC");
        for(int i = 0; i < gos.Length; i++)
        {
            Debug.Log("name:" + gos[i].name);
        }
Input类 
//当A键按下瞬间执行一次
if(Input.GetKeyDown(KeyCode.A)){
    Debug.Log("A");
}
//当B键按下时执行,执行次数大于1
if(Input.GetKey(KeyCode.B)){
    Debug.Log("B");
}
//C键被按下抬起瞬间执行一次
if(Input.GetKeyUp(KeyCode.C)){
    Debug.Log("C");
}

if(Input.GetMouseButton(1)){
    Debug.Log(1);
}
if(Input.GetMouseButton(2)){
    Debug.Log(2);
}
if(Input.GetMouseButton(0)){
    Debug.Log(0);
}

//按下A键 从0到-1 会有很多随机的小数,渐变到-1
//float h=Input.GetAxis("Horizontal");
//float v=Input.GetAxis("Vertical");
//Debug.Log("h:"+h+"V:"+v);

//值在-1 0 1 三个值之间切换,不会有中间值进行渐变
//float h1=Input.GetAxisRaw("Horizontal");
//float v1=Input.GetAxisRaw("Vertical");
//Debug.Log("h1:"+h1+",V1:"+v1);

//检测鼠标左键
if(Input.GetMouseDown("Fire1")){
    Debug.Log("Fire1");

实例:坦克的运动

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

public class TankController : MonoBehaviour
{
    public float speed=0.5f;
    private float h, v;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        h = Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");

        transform.Rotate(0, h, 0);
        transform.Translate(Vector3.forward * v * speed,Space.Self);
    }
}

运行结果:

该系列专栏为网课课程笔记,仅用于学习参考。 


http://www.kler.cn/news/336605.html

相关文章:

  • GraphRAG-Local-UI - 基于 GraphRAG 支持本地的聊天UI
  • SAP 投资 1200 万新元推动新加坡的人工智能创新
  • 回溯算法解决排列组合及子集问题
  • 滚雪球学MySQL[5.2讲]:并发事务的处理
  • 如何在Windows和Linux查看正在监听的端口和绑定的进程
  • JS 入门
  • LabVIEW提高开发效率技巧----使用动态事件
  • 57.对称二叉树
  • 利用SpringBoot框架开发星之语明星周边商城
  • 使用树莓派搭建音乐服务器
  • 【C#生态园】构建安全可靠的身份验证:六种C# OAuth认证库全面比较
  • 数学公式编辑器免费版下载,mathtype和latex哪个好用
  • RestClientException异常
  • 代码随想录算法训练营Day26 | 669. 修剪二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树
  • Spring MVC的工作原理及配置。Spring Boot的自动配置与快速开发。
  • 毕业设计——物联网设备管理系统后台原型设计
  • 数据库语句优化
  • 《Linux从小白到高手》理论篇:深入理解Linux的网络管理
  • 六、Java 基础语法(下)
  • Oracle中ADD_MONTHS()函数详解