Unity3D基础 GameObject和Tramsform类

Unity3D中,GameObject和Tramsform类是最基础也是比较重要的类,GameObject类主要掌管各个游戏对象,每一个游戏对象都是一个GameObject,也可以说GameObject就是游戏对象。Tramsform类主要控制对象的位置,大小等属性。下面介绍几个小的案例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//GameObject类
public class text1 : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject Cube;
    public GameObject Sphere;
    public GameObject Capsule;
    void Start()
    {
        Cube = GameObject.Find("Cube");//通过名称查找游戏对象对象
        Sphere = GameObject.Find("/Cube/Sphere");
        Capsule = GameObject.Find("/Cube/Sphere/Capsule");
        gameObjects = GameObject.FindGameObjectsWithTag("Untagged");
        Cube.AddComponent<Rigidbody>();
        Cube.AddComponent<Collider>();//赋予对象各种组件,刚体,碰撞等。
        Capsule.AddComponent<Rigidbody>();
        Capsule.AddComponent<Collider>();
        

    }
    // Update is called once per frame
    void Update()
    {
       
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Tramsform类
public class text1 : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform Cube;
    public Transform player;
    void Start()
    {
        Debug.Log(transform.localPosition);//相对于父物体的坐标
        Debug.Log(transform.localScale);//缩放
        Debug.Log(transform.eulerAngles);//欧拉角

        Debug.Log(transform.right);//x坐标
        Debug.Log(transform.up);//y坐标
        Debug.Log(transform.forward);//z坐标
    }
    // Update is called once per frame
    void Update()
    {
        //transform.Translate(Vector3.forward * Time.deltaTime * 10);//向前移动物体
        //transform.Translate(Vector3.forward, Space.Self);//向前移动 相对于自身轴
        //transform.Translate(transform.forward,Space.World);//向前移动 相对于世界坐标
        //transform.Translate(transform.forward,Space.Self);//向前移动被 相对于自身轴
        transform.position +=new Vector3(1,1,1); //物体当前位置加上向前移动的位置
        Debug.Log(this.transform.position);
        
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值