Week 5
— GDS212 —
— GDS212 —
— TU —
Projecting a camera onto a UI element
I learnt how to do this from a post on the Unity Forums by davinc131 (2016) and a post on Stack Overflow by Malkijan (2020).
To have a scene camera project onto a UI element, I used a Render Texture and assigned it as the target texture for the Main Camera. Then, in my canvas, I created a new Raw Image and set the texture as the Render Texture I just created. To get the specific panel shown in the picture to the right, I resized it to an 8:7 aspect ratio, and then created a panel, set it to black, made it slightly bigger than the Raw Image element and placed it behind the Raw Image element to separate it from the background.
Image: The side-on camera panel, with the RenderImage and Inspector component.
Making a teleporter for a pitfall trap
My approach
The concept for the pitfall trap is pretty simple: the pit below the trap returns the player to the start of the trap. I took a look at some of the code from the MultiCoinChest.cs page I made for project 1 of GAD170 back in trimester 2, more specifically part 1 of Section 3: Making the chest interactive to the player and setting up for Section, where the trigger functionality for the chest is set. Borrowing some of the code from there, I set up the following code below (comments removed):
public PlayerControllerThatLevelsUp playerControllerThatLevelsUp;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.GetComponent<PlayerControllerThatLevelsUp>())
{
Debug.Log("Trigger activated!");
other.gameObject.transform.position = new Vector3(-2f,6.1f,10.961f);
One thing with this code is that the way it teleports the player back to the start is not the method I intended to use. I used this method as a temporary solution to get the trap working, and I then fixed this issue with Glen's approach.
Image: The greyboxed tight-walk platform obstacle with the pitfall.
Glen's approach
With the help of Glen, I was able to implement a permanent method of resetting the player's position back to the start of the level. What Glen did was make use of the Spawner GameObject I placed in the scene to get the point that the player is to teleport back to, and then offset the respawn position on the Y axis so that the player doesn't get stuck in the floor. This is what those changes look like:
public PlayerControllerThatLevelsUp playerControllerThatLevelsUp;
public GameObject spawner;
public float spawnOffset;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.GetComponent<PlayerControllerThatLevelsUp>())
{
Debug.Log("Trigger activated!");
other.gameObject.transform.position = spawner.transform.position;
other.gameObject.transform.Translate(0,spawnOffset,0);
In the future, I may have to change this code again to take into consideration a variation in the player's height, which has been commented into the script as a TODO (which puts the comment on a task list), but for now, this pretty much solves all the problems in the script and produces a simple pitfall mechanic.
davinc131. (2016). Display image from a camera in a UI component. Unity Forums. Retrieved March 9, 2023 from https://forum.unity.com/threads/display-image-from-a-camera-in-a-ui-component.439289/
Malkijan. (2020). How to set Camera View a Specific Area in UI. Stack Overflow. Retrieved March 9, 2023 from https://stackoverflow.com/questions/61893951/how-to-set-camera-view-a-specific-area-in-ui