本帖最后由 da11 于 2021-9-25 17:08 编辑
//补充:坐标系练习2:在1楼代码的基础上,增加要求:左边越界了,立马在右边回来,反之,右边越界了,立马在左边回来。上下同理。
//自做练习代码:
private Camera cameraVar; //存放相机类型变量
Vector3 ScreenVar; //存放屏幕坐标系的值变量
private float speedF; //旋转速度变量
private float speedWalk; //移动速度变量
private float HorizontalCache = 0; //InputManager左右移动缓存值
private float VerticalCache = 0; //InputManager上下移动缓存值
private bool RightBool = true; //右方向状态值--练习1变量
private bool leftBool = true; //左方向状态值--练习1变量
private bool GoBool = true; //前进方向状态值--练习1变量
private bool BackBool = true; //后退方向状态值--练习1变量
private void Start()
{
//赋值各移动速度
speedF = 1F;
speedWalk = 0.1F;
//Camera.main为获取主相机的方法,相机中的标签必须为MainCamera才能使用此方法,否则请使用另外的获取相机的方法!
cameraVar = Camera.main;
//物体自身position世界坐标转换成摄像机坐标并赋值 Vector3类型 的 ScreenVar变量
ScreenVar = cameraVar.WorldToViewportPoint(this.transform.position);
}
private void Update()
{
//判断屏幕坐标,防止越界移动--坐标系练习!!------------------------------------
//练习2;左边越界了,立马在右边回来,反之,右边越界了,立马在左边回来。上下同理。
//左右旋转并左右移动,实现方案,先使物体旋转,再使用this.transform.Translate使物体前进
if (Input.GetButton("Horizontal")) //检测虚拟按钮Horizontal,对应的绑定按键为d、a、小键盘右、小键盘左
{
//平面左右旋转,欧拉角使用的是y轴!!
//Debug.Log(HorizontalCache - Input.GetAxis("Horizontal"));
//判定InputManager左右移动缓存值与实际按钮移动值相减是否大于0
//比如不按按钮的时候按钮移动值为0,按a键向左,按钮移动值则往 -1 相减,按d键向右,按钮移动值则往 1 相加。
//既,向左按按钮a键,0 -(-1),肯定大于0,由此判断应该向右旋转。
//既,向右按按钮a键,0 -(1),肯定小于0,由此判断应该向左旋转。
if (HorizontalCache - Input.GetAxis("Horizontal") > 0) //左转
{
//Debug.Log("左转");
//Debug.Log(Input.GetAxis("Horizontal"));
//直接瞬移旋转,实际案例不可取
//this.transform.rotation = Quaternion.Euler(0, 270, 0);
////变速旋转,实际案例常用案例
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, Quaternion.Euler(0, 270, 0), 0.1f);
this.transform.Translate(Vector3.forward * speedWalk);
}
else if (HorizontalCache - Input.GetAxis("Horizontal") < 0) //Input.GetAxis("Horizontal") < 0 右转
{
//Debug.Log("右转");
//直接瞬移旋转,实际案例不可取
//this.transform.rotation = Quaternion.Euler(0, -270, 0);
//变速旋转,实际案例常用案例
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, Quaternion.Euler(0, -270, 0), 0.1f);
this.transform.Translate(Vector3.forward * speedWalk);
}
//转向状态可用控制中枢
if (ScreenVar.x < 0.1) //摄像机坐标的x轴小于0.1,左转可用状态即为false
{
Vector3 ZXZT = Camera.main.ViewportToWorldPoint(new Vector3(0.85F, ScreenVar.y, ScreenVar.z));
this.transform.position = new Vector3(ZXZT.x,ZXZT.y,ZXZT.z);
}
else if (ScreenVar.x > 0.9) //摄像机坐标的x轴大于0.9,右转可用状态即为false
{
//RightBool = false;
Vector3 ZXZT = Camera.main.ViewportToWorldPoint(new Vector3(0.15F, ScreenVar.y, ScreenVar.z));
this.transform.position = new Vector3(ZXZT.x, ZXZT.y, ZXZT.z);
}
}
//前后旋转并前后移动,实现方案,先使物体旋转,再使用this.transform.Translate使物体前进
if (Input.GetButton("Vertical")) //检测虚拟按钮Vertical,对应的绑定按键为s、w、小键盘下、小键盘上
{
//Debug.Log(Input.GetAxis("Vertical"));
//平面前后旋转,Vector3坐标使用的是y轴!!
//判定InputManager前后移动缓存值与实际按钮移动值相减是否大于0
//比如不按按钮的时候按钮移动值为0,按w键向前,按钮移动值则往 -1 相减,按s键向后,按钮移动值则往 1 相加。
//既,向前按按钮w键,0 -(-1),肯定大于0,由此判断应该向前旋转。
//既,向后按按钮s键,0 -(1),肯定小于0,由此判断应该向后旋转。
if (VerticalCache - Input.GetAxis("Vertical") > 0) //后退
{
//Debug.Log("后退");
//直接瞬移旋转,实际案例不可取
//this.transform.rotation = Quaternion.Euler(0, 180, 0);
//变速旋转,实际案例常用案例
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, Quaternion.Euler(0, 180, 0), 0.1f);
this.transform.Translate(Vector3.forward * speedWalk);
}
else if (VerticalCache - Input.GetAxis("Vertical") < 0) //Input.GetAxis("Vertical") < 0 前进
{
//Debug.Log("前进");
//直接瞬移旋转,实际案例不可取
//this.transform.rotation = Quaternion.Euler(0, 0, 0);
//变速旋转,实际案例常用案例
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, Quaternion.Euler(0, 0, 0), 0.1f);
//根据速度匀速前进
this.transform.Translate(Vector3.forward * speedWalk);
}
//转向状态可用控制中枢
if (ScreenVar.y < 0.1) //摄像机坐标的y轴小于0.15,向后转可用状态即为false
{
Vector3 ZXZT = Camera.main.ViewportToWorldPoint(new Vector3(ScreenVar.x, 0.85F, ScreenVar.z));
this.transform.position = new Vector3(ZXZT.x, ZXZT.y, ZXZT.z);
}
else if (ScreenVar.y > 0.9) //摄像机坐标的y轴大于0.9,向前转可用状态即为false
{
Vector3 ZXZT = Camera.main.ViewportToWorldPoint(new Vector3(ScreenVar.x, 0.15F, ScreenVar.z));
this.transform.position = new Vector3(ZXZT.x, ZXZT.y, ZXZT.z);
}
}
}
//案例演示动图,图片较大,请耐心等待:
|