Welcome to the new series of trying to recreate some mechanics of the popular game Cult of The Lamb. This series will use the state machine pattern I wrote the other day. You can check it out before continuing to this series. I hope that with this series you can see the power of programming patterns and how useful they can be.
The first things we need to change from that series is to turn our 2D project into 3D. Unity handles both very similar so the change is going to be smooth. There’s a great reference here.
My approach is to use a 3D world with 2D sprites.
Don’t forget to change the view on the Editor as well. Also add some 3D plane as the floor.
In this quick tutorial we are going to recreate the movement and the camera in very simple steps.
Import the Cinemachine packages using the Package Manager.
Once done, right click in your Hierarchy and add a Virtual Camera
Then change the Follow property to your Player.
For the movement we need to change some components on the player. Change the Rigidbody2D to a Rigidbody and set this properties on it. Also Now we use a SphereCollider and place it around the feet.
On the PlayerStateMachine class change the reference to Rigidbody2D to a Rigidbody and change the signature of the Move function from receiving a Vector2 to a Vector3:
...
[SerializeField] private Rigidbody _rigidbody;
...
public void Move(Vector3 velocity)
{
_rigidbody.velocity = velocity;
}
Under your states folder create a new C# script and name it PlayerMove3DState. Now add this code:
using BerserkPixel.StateMachine;
using UnityEngine;
namespace Player.States
{
[CreateAssetMenu(menuName = "States/Player/Move 3D")]
public class PlayerMove3DState : State<PlayerStateMachine>
{
[SerializeField, Range(250f, 500f)] private float _speed = 300f;
private Vector3 _playerInput;
public override void Tick(float deltaTime)
{
_playerInput = new Vector3(_runner.Movement.x, 0, _runner.Movement.y);
}
public override void FixedTick(float fixedDeltaTime)
{
_runner.Move(_playerInput * (_speed * fixedDeltaTime));
}
public override void ChangeState()
{
if (_playerInput == Vector3.zero)
{
_runner.SetState(typeof(PlayerIdleState));
}
}
}
}
This code is identical to the PlayerMoveState we did on the previous tutorial but we change the _playerInput variable to a Vector3 and set it up on the “x” and “z” axis depending on the users input. This axis are the right and forward respectively, and in 3D they are commonly used to move things around.
Finally, go back to the Editor, right click on your Project panel and create a new PlayerMove3DState Scriptable Object.
On the Player’s gameobject Inspector, assign the Rigidbody and replace the old Move State to this new Move 3D State
and violá! Press play and start moving around!
If the rotation isn’t correct, change the CheckFlipSprite function inside the PlayerStateMachine to rotate with a negative value:
_spriteTransform.Rotate(_spriteTransform.rotation.x, -180f, _spriteTransform.rotation.z);
Did you like this post? Tell us
Leave a comment
Log in with your itch.io account to leave a comment.