1,关于如何制作3D旋转机关
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class xuanzuan : MonoBehaviour
{
public float rotateSpeed;//定义可控旋转变量
void Update()
{
transform.Rotate(Vector3.down , rotateSpeed * Time.deltaTime);//旋转
}
}
其中 transform.Rotate(Vector3.down , rotateSpeed * Time.deltaTime);
Verctor.旋转方向!(用来控制物体的旋转)
扩展小技巧:选中方块+“T” 进行拉伸其中有几何中心,旋转是按几何中心旋转;(旋转方向与拉伸方向相关!)
2,通关关卡切换
快捷进入File/Build Settings : ctrl+shift+B
设置Gold(终点)
如果角色(在角色身上设置脚本)到达终点则进行关卡切换:
先定义一个变量:
public int nextStageIndex;
然后:
private void OnTriggerEnter(Collider collision)
{
if(collision .tag =="Goal")
{
SceneManager.LoadScene(nextStageIndex);//场景编号设置切换场景
}
}
否则无法使用:LoadScene
SceneManager.LoadScene(nextStageIndex);//场景编号设置切换场景
设置完成后角色
这样就能够做到角色进入终点(Goal是我自定义的一个终点),便能够直接切换成该设置的关卡代号!
3、关于如何实现小窗模式(俩摄像头,相当于一个摄像头充当地图如下图所示)
首先创建俩个Camera (相机),这个时候也只会显示一个相机视野,因为其中一个遮在另外一个上面,影响因素:Depth;
由本人实践证明Depth数值哪个大便优先显示哪个, 但是我们现在相机视野大小一样所有只能显示一个相机,这个时候必须写个脚本让Depth大的视图变小便能达到效果!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
//定义枚举类型
public enum HorizontalAlignment { left,center,right};//视界水平方向
public enum VerticalAlignment {top,middle,bottom };//视界垂直方向
public enum ScreenDimensions { pixels,screen_percentage};//像素、屏幕百分比
//定义枚举类型变量
public HorizontalAlignment horizontalAlignment = HorizontalAlignment.left;
public VerticalAlignment verticalAlignment = VerticalAlignment.top;
public ScreenDimensions dimensions = ScreenDimensions.pixels;
public int width = 50;
public int height = 50;
public float xOffset = 0.0f;//x偏移量
public float yOffset = 0.0f;//y偏移量
public bool update = true;
void Start()
{
MoveCamera();
}
void Update()
{
MoveCamera();
}
public int hSize, vSize, hLoc, vLoc;
void MoveCamera()
{
if(dimensions==ScreenDimensions.screen_percentage)//调节视图为指定百分比大小
{
hSize = (int)(width * 0.01f * Screen.width);
vSize = (int)(height * 0.01f * Screen.height);
}
else
{
hSize = height;//调节视图为指定像素大小
vSize = width;
}
if(horizontalAlignment==HorizontalAlignment.left)//水平方向左对齐
{
hLoc = (int)(xOffset * 0.01f * Screen.width);
}
else if (horizontalAlignment == HorizontalAlignment.right)//水平方向右对齐
{
hLoc = (int)((Screen.width-hSize)-(xOffset*0.01f*Screen.width));
}
else//水平居中
{
hLoc = (int)((Screen.width - hSize) * 0.5f - (xOffset * 0.01f * Screen.width));
}
if(verticalAlignment ==VerticalAlignment.top)//垂直方向为顶端
{
vLoc = (int)((Screen.height - vSize) - (yOffset * 0.01f * Screen.height));
}
else if(verticalAlignment==VerticalAlignment.bottom)//垂直方向底端
{
vLoc = (int)(yOffset * 0.01f * Screen.height);
}
else//垂直方向为居中
{
vLoc = (int)((Screen.height - vSize) * 0.5f - (yOffset * 0.01f * Screen.height));
}
this.GetComponent<Camera>().pixelRect = new Rect(hLoc, vLoc, hSize, vSize);
}
}
这里的width和height对应好像有点小问题,但是问题不大!
将脚本加在Depth大的上,然后自己调整对应数值!就欧克了
4,关于子物体与父本造成的bug
原本将一个方块变成另一个方块的子物体,结果出现了如下情况:
这样给其施加脚本很容易造成空气墙!(我朋友帮忙测试的时候差点快赢了到了这个地方就撞上空气墙无了)当时就是一把×但是没有很垂直(夹角越小这种现象越明显),所以就会导致上图情况!
后来我删了重新建出现了上图的情况,我想要的效果是这个×旋转式机关(下图),但是一旦他俩不垂直就会出现这种情况!
对于这种情况尽量不要建立子物体关系!这俩个方块应该处于同等地位
我的解决方案:就是不要建立子物体关系(貌似垂直影响就不大),直接将脚本拖给俩个Cuba!或者向下图一样使其处于同等地位!先通过Greate Empty建一个GameObject,将×作为其子物体,然后将脚本拖给GameObject!
这样就能够解决了,所以记得避雷!