旋转机关、关卡设置、俩相机小窗口设置以及部分问题

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 Settingsctrl+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!

这样就能够解决了,所以记得避雷!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

致奋斗的自己

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值