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

34 lines
923 B
C#
Raw Normal View History

2023-04-21 09:30:43 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterControllerForce : MonoBehaviour
{
float mass = 3f; // defines the character mass
Vector3 impact = Vector3.zero;
private CharacterController character;
void Start()
{
character = this.gameObject.GetComponent<CharacterController>();
}
// 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;
}
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);
}
}