Creating a puzzle for accessing the Elevator.
In the first step, create a script called PowerCore and create these variables.
We want this Object to interact with the player. To do this, we use the Interact method, which is inherited from IInteractable.
Go Inside Interactable and check if _isPickedUp is false and set isKinematic to false, set _isPickedUp to true, then set the PowerCore position and Rotation and parent to _playerPickupTransform.
When the player presses E again, we want to drop this power core. To do this, we set isKinematic and _isPickedUp to false and reset the position to the world. We just set the PowerCore parent to null.
Create a script called PowerBase with a public virtual OnCollisionEnter function. Use the Collision parameter to set the PowerCore's position and rotation to match the Base position and rotation.
Create a script called PowerLevel which inherits Scriptable Object, with a public int variable called amount. Then create a public method with a collision parameter, then use that parameter to get the PowerCore component, then increment the powerlevel from the PowerCore by the amount.
Go to PowerCore variable, create a public method called Change Color, then change color according to this percentage.
Create PowerCharger script, inherit the PowerBase, create PowerLevel variable.
Create a New script called PowerReceiver, with the following variables.
Then override OnCollisionEnter, after calling base.OnCollisionEnter, check if we collide with PowerCore, then get the power level from PowerCore.
Check if powerCoreValue(from powerCore) is equal to requiredPowerLevel, then set isActivated to true. Loop through targets, then try to get IInteractable, then call the Interact method and pass isActivated as a parameter to power up the elevator.
Add an else if and an if statement after the above if to check if the powerCoreLevel is above requiredPowerLevel or below the requiredPowerLevel
Now override OnCollisionExit by just setting isActivated to false when the PowerCore gameObject is removed from the powerReceiver.This will make the elevator to be malfunction.
Go to the Project folder <Scriptable Objects create three or 4 powerLevel and specify the value.
Select PowerCore and drag and drop the PowerReceiver to the field called target.
Select the powerCharger add charger script, then drag and drop different scriptable objects to the field name powerLevel.
Go to the PowerCore < ChangeColor>. We use the safety guide to change color.
Now let's try our puzzle to see the color change, and how the receiver accepts the powerCore by checking the powerCoreLevel.
Note: You may find an Issue where the camera tries to avoid colliding with the powerCore.By default Main camera is set to avoid collisions with all objects with a physics layer tagged as default.
Solution.
Set the PowerCore object physics layer as Physics Layer or any other layer you specify according to you.
Here it's our puzzle
See you at the next one.
