Unity Portal Similar effect implementation

Source: Internet
Author: User

Briefly

In the portal, the core of the game is to open on the ground or wall 2 can be connected to the hole to achieve the effect of transmission. This extension joins the puzzle elements to form the core of the game.

Here is an attempt to use unity to implement the core functions of the portal, with the following features:

1. Model of Portal

2. Map of Portal

3. Transmission function of Portal

Here we need the following software: 3dmax 2014,unity3d 5.4.0,visual Studio 2015

I. Model and UV of portal

Open 3dmax, confirm system units and display units are meters, if not for meters, change to meters

Create a cylinder, the height segment number 1, the end segment 1, the number of edges 32, adjust to the appropriate size, convert to editable poly, delete all except the top face, position 0. Convert to editable poly again.

Use the UVW expansion command for UV expansion

Change the name of the model and the name of the material, export the FBX file

Second, the portal dynamic map 1, Static effect realization

Open unity, import models, import an appropriate scenario package as a test scenario, create a keyboard-controlled player for scene travel and functional testing

Create a new camera and place it in another location as the target point for the transfer. Create a rendertexture and drag it onto the target texture of the camera. Change the camera's clear flag to solid color. Change the camera's viewport Rect appropriately.

Drag the imported portal model into the scene, place it on a wall, and set the map of the model material to rendertexture.

You can see the effect as follows

2. Dynamic Effect Realization

The effect of the portal is similar to that of a hole, in a different position to see the past, the size of the display and the objects are very different. Similar to the world you see outside through a window. Therefore, we have to set the relative position of the camera relative to the gate and the corresponding data, such as the angle of view, position, selection and cutting, and so on.

    • The distance between the target camera and the transfer point = the distance between the player and the portal
    • Clipping distance of the target camera = distance between the target camera and the delivery point
    • The target camera is always facing the transfer point
    • Target camera's viewing angle =2*atan (portal height/2/distance of target camera and transfer point)

Note that Atan is radians in unity and needs to be mathf.rad2deg to turn into angles.

Some of the code is as follows (this is where the parent-child relationship is used to change the position, the target camera is the sub-object of the transfer point, so the localposition of the target camera is relative to the transfer point)

Internal protected voidUpdate () {varCPOs =Camera.main.transform.position; varMT =Render.worldtolocalmatrix; Mt= Matrix4x4.trs (Vector3.zero, Quaternion.angleaxis ( the, vector3.up), Vector3.one) *MT; Camera.transform.localPosition=Mt.    Multiplypoint (CPOs);    Camera.transform.LookAt (point); Camera.nearclipplane =-camera.transform.localposition.z;Const floatRenderheight =3f; Camera.fieldofview=2* Mathf.atan (Renderheight/2/camera.nearclipplane) *mathf.rad2deg;}
Third, the transmission function realization

Add a collider to the portal, set to trigger, and set the player's location data when the player collides with it

    • Player's position = location of the transfer point,
    • The player's orientation to the transfer point = the player's orientation to the portal before transmission

Some of the code below

Internal protected void Ontriggerenter (Collider other) {    = other.transform.root.rotation * Point.rotation *  render.rotation;     = Point.position;}
Summarize

The whole project is not very difficult, it takes about 2 days

Currently the project is open source, the address is http://git.oschina.net/CodeGize/PortalGame, can go to download

Demo Address: http://v.youku.com/v_show/id_XMTY5MzE3MzU0MA==.html

Unity Portal Similar effect implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.