unity学习6-vector3

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

public class Lesson6 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

        #region Transform 主要用来干嘛
        #endregion


        #region 必备知识点 vector3基础

        Vector3 v = new Vector3();
        v.x = 10;
        v.y = 10;
        v.z = 10;

        Vector3 v2 = new Vector3(10, 10);//只传入x,y,z默认为0

        Vector3 v3 = new Vector3(10, 10, 10);

        Vector3 v4;
        v4.x = 10;
        v4.y = 10;
        v4.z = 10;


        //基本计算 // + - * /
        Vector3 v1 = new Vector3(1, 1, 1);
        Vector3 v12 = new Vector3(2, 2, 2);
        print(v1 + v12);
        print(v1 - v12);




        //常用的
        print(Vector3.zero);//000
        print(Vector3.right);//100
        print(Vector3.left);//-100
        print(Vector3.forward);//001
        print(Vector3.back);//00-1
        print(Vector3.up);//010
        print(Vector3.down);//0-10


        //计算两个点的距离
        print(Vector3.Distance(v1, v12));


        #endregion

        #region 位置
        //相对世界坐标系
        //this.transform.position
        //通过position得到的位置 是相对于 世界坐标系的 原点的位置
        //可能与面板上显示的不同
        //因为如果有父子关系,并且父对象位置 不在原点 那么和面板上的位置就不一样
        print(this.transform.position);


        //相对父对象的位置
        //相想以面板坐标为准进行设置 那么就要使用localPosition
        print(this.transform.localPosition);


        //注意:位置的赋值不能直接改变zyx,只能改变整体
        //this.transform.position.x = 10;//会报错
        this.transform.position = new Vector3(10, 10, 10);
        this.transform.localPosition = Vector3.up * 10;


        //如果想改一个值x,yz 保持原有值
        //1.直接赋值
        this.transform.position = new Vector3(19, this.transform.position.y, this.transform.position.z);
        //2.先取出来 在赋值
        Vector3 pos = this.transform.localPosition;
        pos.x = 10;
        this.transform.localPosition = pos;

          


        //对象当前各朝向
        //对象的面朝向
        print(this.transform.forward);

        //对象的上朝向
        print(this.transform.up);
        //对象的右朝向
        print(this.transform.right);

        #endregion
       

    }

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

        #region 位移
        //路程 = 方向 * 速度 * 时间

        //方式一 手动计算
        // 当前位置 +  我要移动多长距离  得出最终所在的位置
        //this.transform.position = this.transform.position
        //     + this.transform.forward * 1 * Time.deltaTime;


        //用的是this.transform.forward 所以它始终会朝向相对于自己的面朝向去动
        //this.transform.position += this.transform.forward * 1 * Time.deltaTime;
        //就会朝着世界坐标中的Z轴去移动
        //this.transform.position += Vector3.forward * 1 * Time.deltaTime;


        //方式二 通过API
        //世界坐标:Space.World,本地坐标Space.Self
        //参数一 表示位移多少 (方向 * 速度 * 时间),参数二表示是相对于世界坐标还是相对坐标
        //this.transform.Translate(Vector3.forward * 1 * Time.deltaTime);//默认是相对坐标


        //根据世界坐标来移动,用的 Vector3.forward  及  Space.World
        //this.transform.Translate(Vector3.forward * 1 * Time.deltaTime,Space.World);//相对于世界坐标系的Z轴 动,始终朝着 世界坐标系的Z轴正方向移动



        //根据相对坐标来移动,用的 this.transform.forward  及  Space.World,虽然是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轴移动, 始终朝着 自己的Z轴正方向移动
        this.transform.Translate(Vector3.forward * 1 * Time.deltaTime,Space.Self);


        #endregion




    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值