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
}
}
unity学习6-vector3
最新推荐文章于 2025-09-08 08:00:00 发布
861

被折叠的 条评论
为什么被折叠?



