/unity-genshin-impact-movement-system

尝试复刻原神动作系统

Primary LanguageC#MIT LicenseMIT

Genshin Impact Movement System

An attempt on replicating 3 (Moving, Gliding, Swimming) Genshin Impact Movement Systems.

Overview

Engine

Unity

Available On

YouTube GitHub

Used Assets

Provided Animations were downloaded from Mixamo.

Map Materials were downloaded from Kenneys Assets.

Tips

  • If you intend to Swap Animations, make sure you copy their Animation Events, otherwise some transitions won't work.
  • There's no particular reason why I made the Model "Generic" besides me not understanding enough about Animations.
  • There are no Control Schemes in the Input Actions, so Input only works for Keyboard & Mouse.
  • Because I use the [field: SerializeField] attribute, Unity 2020+ is required. 2020.3 LTS was used for this project.
  • If you want different settings for a smaller or taller character, you can create a new Player Scriptable Object and set the desired settings.
  • There's a Resizable Capsule Collider Component for you to add to any Game Object you want to resize your collider from the bottom only to be able to use the Floating Capsule technique. It should throw an error once when you first add it as a Component due to the OnValidate method likely being called before Unity serializes the data classes, but it should work fine. You can always initialize the [Serializable] classes in the Initialize method if you don't want that error showing.
  • 2 Layers are added from the Layer number 22 to 23, in hopes that there would be no problem in importing this System to your own project. All scripts have a base namespace and folder as well, so there should be no name conflicts.
  • A camera cursor script was added as an example on how you can enable or disable it. It isn't working like in the game, it's just there for you to have an idea how to get it enabled or disabled.
  • Different Animations can make the movement look better or worse.
  • I'm an amateur game developer, so do not expect the code to be the most elegant looking piece of beautiful meat out there.
  • If there's a Bug, you saw nothing.

Table of Contents

Unveil me!

Tutorial Series

Released code is only up to date to my last released Tutorial Series video.

To get an idea on what the Tutorial Series will end up providing, please refer to This Video.

Series Contents

The main goal of the Series is to teach game developers (mostly beginners) on how can they achieve something similar to Genshin Impact Movement System.

Here's the list of techniques used to make this System possible:

  • Pro Builder

    A Small Test Map was created for this Project.

  • New Input System

    The Old Input System was disabled for this Project.

    Version 1.3.0

  • Cinemachine

    A Virtual Camera was used to achieve the same or close to the same result as Genshin Camera.

    Version 2.8.4*

  • State Machines

    Hierarchy State Machines were used at their simplest implementation (State Inheritance).

  • Floating Capsule

    Allows Resizing the Capsule Collider without the Player falling due to Gravity.

    Makes it possible to climb steps and slopes.

  • Physics-Based Movement

    A Rigidbody was used for all the Player Movement.

    No Built-In Character Controller was used.

  • Animations

    Reusable Sub-State Machines were used for the Animations.

    No Blend Trees were used.

*As of this version, there's a bug that doesn't allow disabling the Cinemachine Input Provider actions, which could be used for disabling the Camera Rotation or the Camera Zoom. It should be fixed in future versions.

Pro Builder

A Small Test Map was created in hopes of being able to test all features this System will provide.

Uses Kenneys Prototype Textures as the Map Materials.

Map

New Input System

The use of Unity New Input System allows the addition of callbacks whenever a key was pressed, changed or unpressed.

These translate to the actions of started, performed and canceled.

Features like only considering the key being pressed after an amount of time has passed is also easily achieved by using an Interaction.

The Camera also uses the New Input System.

The Project does not provide a Control Scheme and only provides you with Keyboard Inputs.

InputSystem.mp4

Systems

The plan is to develop and release 3 Genshin Impact Movement Systems.

Generic badge Means the System has been fully released on both Youtube and Github.

Generic badge Means the System has been fully developed but is awaiting release on both Youtube and / or Github or awaiting for another System release.

Generic badge Means the System is still in Development or requires something to be Fixed before it can be released.

Movement System

Generic badge

Cinemachine

The Player Camera imitates Genshin Impact Camera using Unity Cinemachine Package.

This is very likely what Genshin uses as well.

All 3 Systems have different settings for the Camera but it mostly works the same way.

The Camera offers an Orbital Rotation around the Player.

OrbitalCamera.mp4

Click here for longer video (47MB)

We can Zoom In and Zoom Out using the Mouse Scroll Wheel.

CameraZoom.mp4

Click here for longer video (83.6MB)

It also offers different Rotations at different angles or movement directions.

For example, moving the Player to the sides slowly rotates the Camera, which in itself rotates the Player, as the Player Rotation is relative to the Camera.

SidewaysRotation.mp4

Click here for longer video (31.7MB)

When looking from the Top or Bottom, it also rotates faster, which makes the Player move in a small circular motion (much like in Genshin).

TopdownRotation.mp4

Click here for longer video (120MB)

There are several possible rotation outcomes, all of which I speak of in my Tutorial Series Video about Camera Recentering.

States & State Machines

The main logic of the Player Movement follows the State Machines Pattern.

I've decided to cache the existing States instead of instantiating new ones.

Therefore, changing to another State can be done through a similar code line: stateMachine.ChangeState(stateMachine.IdlingState).

StatesAndStateMachine.mp4

Click here for longer video (42MB)

Floating Capsule

To prevent the Player from getting stuck on steps as well as to prevent the Player from jumping on slopes, a force is constantly added to the Player to keep it afloat.

A resizable capsule collider feature is provided, which is considered the same as the maximum climbable step height.

The size is only removed from the bottom.

ResizableCapsuleCollider.mp4

To prevent the Player from falling due to Gravity when its collider is resized, a Ray is cast downwards and a (positive or negative) force is added to the Player depending on the distance from the collider center to the ground.

This allows the Player to keep itself at a constant height both when moving on slopes or when trying to climb steps.

FloatingCapsule.mp4

Click here for longer video (55.2MB)

The idea was taken From This Video.

Physics-Based Movement

A Rigidbody is used for the Character Movement, meaning that it moves using built-in Physics.

Player Movement

The Ground Movement offers Walking, Running, Dashing, Sprinting, Landing and Stopping States as well as a way to swap between Walking and Running at the press of a Button.

There's also Jumping and Falling.

Movement.mp4

Click here for longer video (95.5MB)

The Player also moves slower depending on the Slope / Ground Angle (An Animation Curve is used to set the Angles and Speeds).

MovementOnSlopes.mp4

Click here for longer video (41.2MB)

The System also offers the transition between Sprinting to Running and Walking when the Sprint key wasn't held long enough to keep performing it.

SprintToRunToWalk.mp4

Player Rotation

The Player rotates relative to the Camera and the Movement Input.

PlayerRotation.mp4

Click here for longer video (27.3MB)

Much like in Genshin, it offers an Automatic Rotation in certain situations.

For example, fast pressing (pressing and releasing right away) a movement key will keep rotating the Player until it's looking towards the desired movement direction.

AutomaticRotation.mp4

Click here for longer video (39.1MB)

Player Dash

The Player can dash up to 2 times (updatable) in less than 1 second (updatable).

If the Player dashes too fast, it will enter a Cooldown (updatable) and the Player will only be able to dash again after that cooldown ends.

Dash.mp4

Click here for longer video (50.7MB)

Player Jump

The Player Jump offers a few different possibilities on where to Jump much like in Genshin.

If there's a Movement Input Key being pressed, the Player will Jump towards the direction of that Movement Key.

JumpMovementDirection.mp4

Click here for longer video (37.2MB)

If there's no Movement Input Key being pressed, the Player will Jump towards the direction it's facing.

JumpFacingDirection.mp4

Click here for longer video (71.5MB)

Furthermore, its force depends on the current Player State as well as the Slope / Ground Angle (An Animation Curve is used to set the Angles and Forces).

This is to ensure that the Player does not slide on Slopes from having too much of an Horizontal Velocity.

JumpOnSlopes.mp4

Click here for longer video (79MB)

Player Fall

When the Player leaves the Ground, it will start Falling.

Fall.mp4

The Jump also transitions to Falling when it reaches its top vertical positive velocity (exactly when the velocity starts becoming negative).

JumpToFall.mp4

A velocity limit is set to make sure it collides with shallow ground colliders correctly.

FallVelocityLimit.mp4

Player Landing

When the Player collides with the Ground coming from an Airborne State, it can enter a Landing State.

The common Landing State between all the Airborne States is the Light Landing State, which happens when the Player falls from a small height.

LightLanding.mp4

Click here for longer video (41.5MB)

When coming from the Falling State, 2 new Landing States are added: Hard Landing and Rolling.

Hard Landing happens when the Player falls from an high height and has no Movement Input Key being pressed at the moment of contact.

HardLanding.mp4

Rolling happens when the Player falls from an high height and has a Movement Input Key being pressed at the moment of contact.

Rolling.mp4

Animations

Animations are added through an Animator Controller using Reusable Sub-State Machines (No Blend Trees are used).

AnimatorController.mp4

There are Animations for every State.

Gliding System

Generic badge

  • State Machine & States (Transitions Included)
  • Glides
  • Different Camera Settings
  • Player Animations (Glide Start Animation Included)
  • Player Tilt (Needs to be fixed)

Swimming System

Generic badge

  • State Machine & States (Transitions Included)
  • Swims (Fast Swim Included)
  • Dives
  • Floats
  • Submerges
  • Different Camera Orbit
  • Different Camera Settings
  • Player Animations
  • Can go back to the Ground (Needs to be fixed in a certain Situation)
  • Movement (Needs to be reviewed to make sure it's working as intended)
  • Rotation (Needs to be fixed in a certain Situation)

License

License

Mixamo Animations & Kenney's Prototype Textures are not considered in my License and are provided just so you can test the System.

Their licenses still apply.

Version

License

Contributing

This Movement System will only be updated as I release more Videos for my Tutorial Series.

If you want to support me, consider subscribing to my Youtube Channel and / or following me on Twitter, both would help me out a ton and be appreciated.

YouTube Twitter

Notes

Animations were downloaded from https://www.mixamo.com.

I use Kenney's Prototype Textures for the Included Map.

As of the moment I released this repository, I'm on a break.

This means that the System won't be updated soon and it might take a while for the rest of the Systems to be released.