简体   繁体   English

将播放器旋转更改为相机旋转

[英]Changing player rotation to a camera rotation

I'm new to unity and I'm currently working on a portal-like game. 我是团结的新手,目前正在开发类似门户的游戏。

I did the whole teleportation script and it works, but the problem comes that I didn't implement the player camera correction and actually I don't have any ideas how to do it. 我完成了整个隐形传送脚本,并且可以正常工作,但是问题来了,我没有实现播放器摄像头校正,实际上我不知道该怎么做。 The concept is that when you're jumping through a portal, the player (or player camera) rotation should be changed to the portal/portal camera rotation from you've come so the final effect is more 'realistic'. 其概念是,当您通过门户跳跃时,应将玩家(或玩家摄像机)的旋转更改为来自您的门户/门户摄像机的旋转,以使最终效果更加“真实”。

I've tried some lines in teleportation script like player.transform.rotation = portal.transform.rotation but in the end it didn't work and now I end up with nothing, deleting previous scripts and trying to write it all over and over again. 我已经尝试了player.transform.rotation = portal.transform.rotation传送脚本中的某些行,例如player.transform.rotation = portal.transform.rotation但是最后它没有用,现在我一无所有,删除了以前的脚本并试图一遍又一遍地编写再次。

I'll be glad if someone could guide me how to start coding it. 如果有人可以指导我如何开始编写代码,我会感到很高兴。 Should I do it in onTriggerEnter (when you're jump through portal), or in onTriggerExit ? 我应该在onTriggerEnter (当您通过门户跳转时)还是在onTriggerExit Should the script be attached to a player or to a portals? 该脚本应该附加到播放器还是门户? Should I gather rotation only from camera or from the whole gameobject (portal/player)? 我应该只从摄像机还是从整个游戏对象(门户/玩家)收集旋转? I'm posting also couple of screens (with a video how it currently works, and also an entire teleportation script. If I missed something just ask me and I'll post it here. 我还发布了几个屏幕(带有一个视频,它当前的工作方式以及整个传送脚本。如果我错过了什么,请问我,我将其发布在这里。

https://imgur.com/a/pbqYnLD - screens with portals inspector https://imgur.com/a/pbqYnLD-具有门户检查器的屏幕

https://streamable.com/b14hk - video how it works https://streamable.com/b14hk-视频工作原理

teleportation script: 隐形传送脚本:

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

public class Teleportation : MonoBehaviour {

    [SerializeField] private GameObject otherPortal;
    [SerializeField] private GameObject player;

    void OnTriggerEnter(Collider col) {

        if(col.tag == "Player") {

            col.transform.position = new Vector3(otherPortal.transform.position.x+1, otherPortal.transform.position.y+1, otherPortal.transform.position.z+1);
            Debug.Log("wszedłem w portal");

        }
    }


    void Update() {

    }

}

some informations how it is coded right now: 一些信息,目前如何编码:

  • portals are currently in game behind 'the box', i didnt instantiate them anywhere; 门户网站目前在“盒子”后面的游戏中,我没有在任何地方实例化它们。 just changing position on lpm (blue portal) and ppm (orange portal) 只是更改了lpm(蓝色门户)和ppm(橙色门户)的位置
  • portals are sticking to the walls, just like in the original game 门户就像原始游戏一样,贴在墙上
  • portals have a camera attached to it and right now the cameras are static. 门户网站已连接了一个摄像头,现在这些摄像头是静态的。 (offtop: i have a script to move them exactly when player is moving and it quite works but also have some problems, like camera can get too far away from portal and start rendering only that green outer side of the box, and i also dont know how to fix it, so currently i didnt use this script) (offtop:我有一个脚本可以在播放器移动时准确地移动它们,它确实可以工作,但是也存在一些问题,例如相机可能离门户太远,只能开始渲染盒子的绿色外侧,而我也没有知道如何解决它,所以目前我没有使用此脚本)
  • the player movement im using is that from unity standard assets (if it somehow matters) 我正在使用的玩家移动是来自统一标准资产(如果有问题的话)
  • the player have a rigidbody but the portals dont; 玩家的身体僵硬,但传送门却没有; not sure if i should attach this component to them 不知道我是否应该将此组件附加到他们
  • teleportation script is attached to the both of portals - the 'otherPortal' variable is moved from inspector, like in orange portal the 'otherPortal' variable is blue portal and the other way 传送脚本附加到两个门户上-'otherPortal'变量从检查器中移出,就像在橙色门户中,'otherPortal'变量是蓝色门户一样,反之亦然

What you did is correct (setting the player rotation to the portal. You can do it in onTriggerEnter after setting the position, then it should look like 您所做的是正确的(将玩家旋转设置为门户。您可以在设置位置后在onTriggerEnter进行操作,然后看起来像

player.transform.rotation = otherPortal.transform.rotation

If you do that, the player will have the same rotation. 如果这样做,播放器将具有相同的旋转角度。 You already have something that make the camera follow the player, so it is likely that you don't need to set the camera rotation. 您已经拥有使照相机跟随播放器的功能,因此可能不需要设置照相机旋转角度。 I don't know how you did your camera follow, so I can't be sure, though. 我不知道您的相机如何操作,所以我不确定。 If the camera has not the proper orientation, doing Camera.main.transform.rotation = otherPortal.transform.rotation will do it. 如果摄像机的方向不正确,则执行Camera.main.transform.rotation = otherPortal.transform.rotation

The remaining thing that might be wring, could be that your player (and camera) is not facing the right axis. 剩下的可能是您的播放器(和照相机)没有朝向右轴。 On your video, I can see that the portal faces the x-axis (the red axis in Unity editor). 在您的视频上,我可以看到门户面向x轴(Unity编辑器中的红色轴)。 Check that when going forward, your player has the red axis looking forward. 检查前进时,播放器的红色轴是否向前。

It is likely that your player has the z-axis (blue) facing forward, which is (by convention) more correct and fits the names Unity uses (z-axis is also called forward-axis) 您的播放器很有可能具有z轴(蓝色)朝前,(按照惯例)更正确,并且适​​合Unity使用的名称(z轴也称为前向轴)

I would recomand to create the portal object (and all other objects, including the player) so that the forward-axis is the blue one. 我建议创建门户对象(以及其他所有对象,包括播放器),以使前向轴为蓝色。 It might require editing the objects. 可能需要编辑对象。 Anycase, check that the player forward axis is that same as the portal, otherwise setting the rotation won't work 在任何情况下,请检查玩家前进轴是否与传送门相同,否则无法设置旋转角度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM