Transform的重要方法

位置和位移

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson6 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #region 知识点一 Vector3基础
        //Vector主要是用来表示三维坐标系中的一个点 或者一个向量
        //申明
        Vector3 v = new Vector3();
        v.x = 10;
        v.y = 10;
        v.z = 10;
        //只传x,y,z默认是0
        Vector3 v2 = new Vector3(10, 10);
        //一步到位
        Vector3 v3 = new Vector3(10, 10, 10);
        //vector的基本运算
        print(v2- v3);
        //和正常的+-*/方法一样
        
        //常用
        print(Vector3 .zero);//0 0 0
        print(Vector3 .right );//1 0 0
        print(Vector3.left);//-1 0 0
        print(Vector3 .up );//0 1 0
        print(Vector3 .forward );//0 0 1
        print(Vector3 .back  );//0 0 -1
        print (Vector3 .down  );//0 -1 0
        print (Vector3 .one );//单位向量
        //计算两个点之间距离的方法
        Vector3.Distance(v2, v3);
        #endregion
        #region 知识点二位置
        //相对世界坐标系
        print(this.transform .position);
        //通过position得到的位置 是相对于世界坐标系原点的位置
        //可能和面板上显示的是不一样的
        //因为如果有父子关系 并且父对象的位置不在原点,那么和面板上的肯定不一样

        //相对于父对象
        print(this.transform .localPosition );
        //如果想以面板坐标为准,那就用localPosition
        //相对世界坐标和相对父对象坐标可能一样的情况
        //1.父对象的坐标就是世界原点
        //2.对象没有父对象
        //注意:位置的赋值不能直接改变x,y,z 只能整体改变
        this.transform.position=new Vector3(10,10,10);
        //如果只想改变x,  y,z保持不变
        //1.直接赋值
        this.transform.position = new Vector3(19, this.transform.position.y, this.transform.position.z);
        //2.先取出来,再赋值
        //虽然不能直接改transform的x,y,z值,但是可以直接改Vector的x,y,z值
        //所以 可以先取出来改Vector再改
        Vector3 Vpos = this.transform.position;
        Vpos.x = 13;
        this.transform.position = Vpos;

        //对象当前的各个朝向
        print(this.transform.forward);//对象当前的面朝向
        print(this.transform.up);//对象当前的头顶朝向
        print(this.transform.right);//对象当前的右手朝向
                                    //如果你想得到对象当前的一个朝向
                                    //那么就通过transform.出来的
        #endregion
       
    }

    // Update is called once per frame
    void Update()
    {
        #region 知识点三 位移
        //理解坐标系下的位移计算公式
        //路程=方向*速度*时间

        //方式一 自己计算
        //当前的位置+移动的距离=最终的位置
        //this.transform.position += this.transform.forward * 1 * Time.deltaTime;
        //方向非常的重要,他直接决定了你的移动方向
        //沿世界坐标系方向进行移动
        //this.transform.position += Vector3.forward * 1 * Time.deltaTime;

        //方式二 API
        //参数一:表示位移的多少 
        //参数二:表示相对坐标系     默认是相对于自己坐标系的
        //相对于世界坐标系的z轴移动
        //this.transform .Translate (Vector3 .forward *1*Time .deltaTime,Space.World);

        //相对于世界坐标的 自己的面朝向去移动
        this.transform.Translate(this.transform.forward*1*Time .deltaTime,Space.World);
        //相对于自己坐标系 自己的面朝向移动(一定不会这样让物体移动)
        this.transform.Translate(this.transform.forward * 1 * Time.deltaTime, Space.Self);
        //相对于自己坐标系 Z轴朝向移动
        this.transform.Translate(Vector3.forward * 1 * Time.deltaTime, Space.Self );
        //注意:一般使用API来位移
        #endregion
    }
}