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/CharacterControllerForce.cs

30 lines
887 B
C#
Raw Normal View History

2023-04-21 09:30:43 +02:00
using UnityEngine;
public class CharacterControllerForce : MonoBehaviour
{
private CharacterController character;
2023-06-01 17:03:48 +02:00
private Vector3 impact = Vector3.zero;
private readonly float mass = 3f; // defines the character mass
2023-04-21 09:30:43 +02:00
2023-06-01 17:03:48 +02:00
private void Start()
2023-04-21 09:30:43 +02:00
{
2023-06-01 17:03:48 +02:00
character = gameObject.GetComponent<CharacterController>();
2023-04-21 09:30:43 +02:00
}
2023-06-01 17:03:48 +02:00
private void Update()
2023-04-21 09:30:43 +02:00
{
// apply the impact force:
2023-06-01 17:03:48 +02:00
if (impact.magnitude > 0.2)
2023-04-21 09:30:43 +02:00
character.Move(impact * Time.deltaTime);
// consumes the impact energy each cycle:
impact = Vector3.Lerp(impact, Vector3.zero, 5 * Time.deltaTime);
}
2023-06-01 17:03:48 +02:00
// 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;
}
}