This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Legacy/CharacterControllerForce.cs
MarcoHampel f8590432ba Assemblies Made and Refactored Code Folders
Created assemblies for the new design code. Relocated legacy scripts into a legacy folder and made a "new_design" folder for new design.
2023-09-11 19:39:27 -04:00

30 lines
887 B
C#

using UnityEngine;
public class CharacterControllerForce : MonoBehaviour
{
private CharacterController character;
private Vector3 impact = Vector3.zero;
private readonly float mass = 3f; // defines the character mass
private void Start()
{
character = gameObject.GetComponent<CharacterController>();
}
private void Update()
{
// apply the impact force:
if (impact.magnitude > 0.2)
character.Move(impact * Time.deltaTime);
// consumes the impact energy each cycle:
impact = Vector3.Lerp(impact, Vector3.zero, 5 * Time.deltaTime);
}
// call this function to add an impact force:
public void AddImpact(Vector3 dir, float force)
{
dir.Normalize();
if (dir.y < 0) dir.y = -dir.y; // reflect down force on the ground
impact += dir.normalized * force / mass;
}
}