Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltwo committed Feb 27, 2024
0 parents commit 956435b
Show file tree
Hide file tree
Showing 72 changed files with 5,709 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 DAIKI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ActionGameCore
Base scripts for 3D action game.
71 changes: 71 additions & 0 deletions src/ActionGameCore/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.aab
*.unitypackage

# Crashlytics generated file
crashlytics-build.properties

# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*

# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*
6 changes: 6 additions & 0 deletions src/ActionGameCore/.vsconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": "1.0",
"components": [
"Microsoft.VisualStudio.Workload.ManagedGame"
]
}
8 changes: 8 additions & 0 deletions src/ActionGameCore/Assets/CharacterControls.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/ActionGameCore/Assets/CharacterControls/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## [0.5.0] - 2024-02-27
### Added
- Add character stay and walk.
7 changes: 7 additions & 0 deletions src/ActionGameCore/Assets/CharacterControls/CHANGELOG.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/ActionGameCore/Assets/CharacterControls/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "CharacterControls.Inputs",
"rootNamespace": "",
"references": [
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:9cb36de80f6aa0e4f925e2361fb85950"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [
{
"name": "com.unity.inputsystem",
"expression": "",
"define": "SUPPORT_INPUTSYSTEM"
}
],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#if SUPPORT_INPUTSYSTEM
using CharacterControls.Movements;
using UnityEngine;
using UnityEngine.InputSystem;

namespace CharacterControls.Inputs
{
public class CharacterMoveInput : MonoBehaviour
{
[SerializeField]
public InputActionAsset InputActionAsset;

public IMoveController MoveController { get; set; }

private void Start()
{
MoveController = GetComponent<IMoveController>();
}

private void Update()
{
if (InputActionAsset == null || MoveController == null)
{
return;
}

InputActionAsset.Enable();
var map = InputActionAsset.FindActionMap("Player");
var move = map.FindAction("Move");
var value = move.ReadValue<Vector2>();
MoveController.SetMoveInput(value);
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "CharacterControls.Movements",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using UnityEngine;

namespace CharacterControls.Movements
{
public class CharacterMoveController : MonoBehaviour, IMoveController
{
[SerializeField]
public Rigidbody Rigidbody = null;

[SerializeField]
public Transform CameraTransform = null;

[SerializeField]
public float StepHeightMax = 0.25f;

[SerializeField]
private bool _autoResizeCapsuleCollider = true;

[SerializeField]
private CapsuleCollider _capsuleCollider = null;

[SerializeField]
public float LegStrength = 40.0f;

[SerializeField]
public float LegSuspenion = 20.0f;

[SerializeField]
public float WalkSpeed = 5.0f;

private Vector2 _moveInput;

private void Reset()
{
Rigidbody = GetComponent<Rigidbody>();
CameraTransform = Camera.main?.transform;
_capsuleCollider = GetComponent<CapsuleCollider>();
}

private void Start()
{
if (_autoResizeCapsuleCollider && _capsuleCollider != null)
{
var height = _capsuleCollider.height;
var center = _capsuleCollider.center;
var stepHeight = StepHeightMax;
var newHeight = height - stepHeight;
var newCenter = center + new Vector3(0, stepHeight / 2, 0);
_capsuleCollider.height = newHeight;
_capsuleCollider.center = newCenter;
}
}

public void SetMoveInput(Vector2 moveInput)
{
if (moveInput.sqrMagnitude > 1)
{
moveInput.Normalize();
}

_moveInput = moveInput;
}

private void FixedUpdate()
{
if (Rigidbody == null)
{
return;
}

const float DistanceMergin = 0.1f;
var legRay = new Ray(transform.position + transform.up * (StepHeightMax + DistanceMergin), -transform.up);
if (Physics.Raycast(legRay, out var hitInfo, StepHeightMax + DistanceMergin))
{
// Leg spring
var hitDistance = hitInfo.distance - DistanceMergin;
var springRatio = Mathf.Clamp01(hitDistance / StepHeightMax);
var springPushForce = (1 - springRatio) * LegStrength;
var suspencionForce = Vector3.Project(Rigidbody.velocity, transform.up) * -LegSuspenion;
Rigidbody.AddForce(springPushForce * transform.up + suspencionForce, ForceMode.Acceleration);

// Move horizontal
var forward = GetForwardOfMovementSpace();
var right = Vector3.Cross(transform.up, forward);
var targetVelocity = (forward * _moveInput.y + right * _moveInput.x) * WalkSpeed;
var currentVerticalVelocity = Vector3.Project(Rigidbody.velocity, transform.up);
Rigidbody.velocity = targetVelocity + currentVerticalVelocity;
}
}

private Vector3 GetForwardOfMovementSpace()
{
if (CameraTransform == null)
{
return transform.forward;
}

var forward = CameraTransform.forward;
if (forward == transform.up)
{
forward = -CameraTransform.up;
}
else if (forward == -transform.up)
{
forward = CameraTransform.up;
}

return Vector3.ProjectOnPlane(forward, transform.up).normalized;
}

private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, transform.position + transform.up * StepHeightMax);
}
}
}
Loading

0 comments on commit 956435b

Please sign in to comment.