34 lines
923 B
C#
34 lines
923 B
C#
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);
|
|
}
|
|
}
|