简体   繁体   中英

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.

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 ? 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://streamable.com/b14hk - video how it works

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)
  • 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)
  • 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

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

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.

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). 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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