Unity project focused on building a solid foundation for 2d movement (video demo) - feel free to use it!
Last tested version of Unity: 2020.1.13.f1
✅ No rigidbodies / No 2d physics (allows for tight controls)
✅ Horizontal and vertical collisions
✅ Ascend and descend slopes (constant speed)
✅ Variable jump height
✅ Wall jumps
✅ Wall sliding
✅ Wall grab
✅ Slide down steep slopes
✅ Jump through platforms
✅ Moving platforms (moves passengers as well)
Movement:
A/D: Left/Right movement
W: Jump
In air:
S: Fast Fall
On wall (facing left):
W: Wall jump
A+W: Wall jump climb
D+W: Wall leap off
Opposite controls for facing right
N.B. You need to enable "Auto Sync Transformations" under the Physics 2D Project Settings.
Here's a quick run down of the important scripts/variables:
Scripts:
- PlayerInput script handles controls.
- PlayerVelocity script takes input from the PlayerInput and calculates velocities then outputs displacements.
- Movement takes input displacements (e.g. from PlayerVelocity/PassengerMover) then checks for collisions and moves the given object accordingly.
- MovingPlatform moves a platform, you can also assign the platform to be a PassengerMover then it will move any other objects that have been assigned as a Passenger.
- BoxColliderCasts is a base class to set up raycasts and boxcast origins for collision detection.
Adjustable Variables:
- PlayerVelocity allows you to adjust:
- Move speed (Left/Right movement)
- Jump heights and time to reach jump height (calculates jump speed / gravity)
- Acceleration time on ground and in air (calculates drag on ground and in air)
- Fast fall speed
- Wall jump, jump climb and leap off velocities
- Wall slide speed
- Wall stick time
- Movement allows you to adjust:
- Collision mask - what layers the object should collide with
- Skin width - small tolerance given to collision detection
- Distance between rays - smaller givens better detection but more computation
- MovingPlatform allows you to adjust:
- Waypoints to travel through
- Platform's travel speed
- Cycle through waypoints or not
- Wait time at waypoints
- Easing
- PassengerMover allows you to adjust:
- Passenger mask - what layer objects should be moved
- Collision mask, Skin width and Distance between rays
Layers/Tags:
- Through tag should be applied to platforms that can be jumped through
- Player/Enemies/Allies layers can be used to allow collision or not as you wish
- If Player/Enemy/Ally tag used, then object requires Movement to be able to detect collision and move correctly.
This was original made by following a Sebastian Lague video series, but has been heavily adapted to be tighter/smoother. If you'd like to start from scratch or don't understand components of the project I'd suggest using the videos as a guide!
Please feel free to post any issues or submit any pull requests and I'll get on em asap :)