moar shit
This commit is contained in:
@@ -1,73 +1,114 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CharacterEngine.Primitives
|
||||
{
|
||||
public sealed class CECollider
|
||||
public class CECollider
|
||||
{
|
||||
public static int[,] edges = new int[,]
|
||||
{
|
||||
{0, 4}, {0, 2}, {0, 6},
|
||||
{1, 7}, {1, 5}, {1, 3},
|
||||
{3, 4}, {3, 2}, {6, 5},
|
||||
{6, 7}, {7, 2}, {5, 4}
|
||||
{0, 2}, {0, 3}, {1, 2},{1, 3}
|
||||
};
|
||||
|
||||
public static int[,] faces = new int[,]
|
||||
{
|
||||
{0, 3}, {0, 5}, {0, 7},
|
||||
{1, 6}, {1, 4}, {1, 2}
|
||||
};
|
||||
|
||||
private Collider collider;
|
||||
public Collider Coll
|
||||
|
||||
private BoxCollider2D collider;
|
||||
public BoxCollider2D Coll
|
||||
{
|
||||
get { return collider; }
|
||||
}
|
||||
|
||||
public Vector3 position
|
||||
public virtual Vector2 position
|
||||
{
|
||||
get { return collider.bounds.center; }
|
||||
}
|
||||
|
||||
public readonly Vector3[] localPoints;
|
||||
public float Height
|
||||
{
|
||||
get { return (GetPoint(0) - GetPoint(3)).magnitude; }
|
||||
}
|
||||
public float Width
|
||||
{
|
||||
get { return (GetPoint(0) - GetPoint(2)).magnitude; }
|
||||
}
|
||||
|
||||
public CECollider (Collider coll)
|
||||
public Vector2 Max
|
||||
{
|
||||
get { return GetPoint(0); }
|
||||
}
|
||||
public Vector2 Min
|
||||
{
|
||||
get { return GetPoint(1); }
|
||||
}
|
||||
|
||||
public Vector2 AABBMax
|
||||
{
|
||||
get { return GetAABBPoint(0); }
|
||||
}
|
||||
public Vector2 AABBMin
|
||||
{
|
||||
get { return GetAABBPoint(1); }
|
||||
}
|
||||
|
||||
public readonly Vector2[] localPoints;
|
||||
|
||||
public CECollider (BoxCollider2D coll)
|
||||
{
|
||||
collider = coll;
|
||||
|
||||
///COLLIDER CONSTANT RELATIVE POINTS
|
||||
localPoints = new Vector3[]
|
||||
localPoints = new Vector2[]
|
||||
{
|
||||
coll.bounds.size * 0.5f, //max
|
||||
-coll.bounds.size * 0.5f, //min
|
||||
new Vector3(-coll.bounds.extents.x, coll.bounds.extents.y, coll.bounds.extents.z),
|
||||
new Vector3(-coll.bounds.extents.x, coll.bounds.extents.y, -coll.bounds.extents.z),
|
||||
new Vector3(coll.bounds.extents.x, coll.bounds.extents.y, -coll.bounds.extents.z),
|
||||
new Vector3(coll.bounds.extents.x, -coll.bounds.extents.y, -coll.bounds.extents.z),
|
||||
new Vector3(coll.bounds.extents.x, -coll.bounds.extents.y, coll.bounds.extents.z),
|
||||
new Vector3(-coll.bounds.extents.x, -coll.bounds.extents.y, coll.bounds.extents.z)
|
||||
coll.size * 0.5f, //max
|
||||
-coll.size * 0.5f, //min
|
||||
new Vector2(-coll.size.x * 0.5f, coll.size.y * 0.5f),
|
||||
new Vector2(coll.size.x * 0.5f, -coll.size.y * 0.5f)
|
||||
};
|
||||
}
|
||||
|
||||
public Vector3 GetPoint (int index)
|
||||
/// <summary>
|
||||
/// GET REAL WORLD POINT OF COLLIDER
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Vector2 GetPoint (int index)
|
||||
{
|
||||
return position + localPoints[index];
|
||||
return position + (Vector2)(collider.transform.rotation * Vector2.Scale(localPoints[index], collider.transform.lossyScale));
|
||||
}
|
||||
public virtual Vector2 GetPoint(int index, Vector2 position)
|
||||
{
|
||||
return position + (Vector2)(collider.transform.rotation * Vector2.Scale(localPoints[index], collider.transform.lossyScale));
|
||||
}
|
||||
public virtual Vector2 GetAABBPoint(int index)
|
||||
{
|
||||
return position + (Vector2)(Vector2.Scale(localPoints[index], collider.transform.lossyScale));
|
||||
}
|
||||
public virtual Vector2 GetAABBPoint(int index, Vector2 position)
|
||||
{
|
||||
return position + (Vector2)(Vector2.Scale(localPoints[index], collider.transform.lossyScale));
|
||||
}
|
||||
|
||||
public bool ContainsPoint (Vector2 p)
|
||||
{
|
||||
p = position + (Vector2)(Quaternion.Inverse(collider.transform.rotation) * (p - position));
|
||||
bool insideX = p.x > AABBMin.x && p.x < AABBMax.x;
|
||||
bool insideY = p.y > AABBMin.y && p.y < AABBMax.y;
|
||||
|
||||
return insideX && insideY;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Collision test along a path
|
||||
/// </summary>
|
||||
public RaycastHit SweepTest(Vector3 dir, float distance, float skinWidth = 1.0f)
|
||||
public CECollision SweepTest(Vector2 dir, float distance)
|
||||
{
|
||||
RaycastHit hit = new RaycastHit();
|
||||
CECollision hit = new CECollision();
|
||||
|
||||
for (int i = 0; i <= CEPrimitives.sweepResolution; i++)
|
||||
{
|
||||
Vector3 currentDistance = dir.normalized * (distance * (i / CEPrimitives.sweepResolution));
|
||||
hit = BoxCast(position + currentDistance, skinWidth);
|
||||
Vector2 currentDistance = dir.normalized * (distance * (i / CEPrimitives.sweepResolution));
|
||||
hit = BoxCast(position + currentDistance);
|
||||
|
||||
if (hit.distance != 0.0f) break;
|
||||
if (hit.impulse.magnitude != 0.0f) break;
|
||||
}
|
||||
|
||||
return hit;
|
||||
@@ -76,45 +117,51 @@ namespace CharacterEngine.Primitives
|
||||
/// <summary>
|
||||
/// Collision test in an specific point
|
||||
/// </summary>
|
||||
public RaycastHit BoxCast(Vector3 position, float skinWidth = 1.0f)
|
||||
public CECollision BoxCast(Vector2 position)
|
||||
{
|
||||
int mask = LayerMask.GetMask("Default");
|
||||
|
||||
RaycastHit outHit = new RaycastHit();
|
||||
CECollision collision = new CECollision();
|
||||
RaycastHit2D outHit = new RaycastHit2D();
|
||||
|
||||
|
||||
///FACE CAST
|
||||
for (int i = 0; i < faces.GetLength(0); i++)
|
||||
for (int i = 0; i < edges.GetLength(0); i++)
|
||||
{
|
||||
//Debug.DrawLine(points[faces[i, 0]], points[faces[i, 1]], Color.blue);
|
||||
outHit = Physics2D.Linecast(GetPoint(edges[i, 0], position), GetPoint(edges[i, 1], position), mask);
|
||||
|
||||
if (!Physics.Linecast(position + localPoints[faces[i, 0]] * skinWidth, position + localPoints[faces[i, 1]] * skinWidth, out outHit, mask, QueryTriggerInteraction.Ignore))
|
||||
if (!outHit)
|
||||
{
|
||||
continue;
|
||||
outHit = Physics2D.Linecast(GetPoint(edges[i, 1], position), GetPoint(edges[i, 0], position), mask);
|
||||
}
|
||||
|
||||
///RETURN DISTANCE FROM CENTER
|
||||
return outHit;
|
||||
if (outHit) break;
|
||||
}
|
||||
|
||||
///EDGE CAST
|
||||
for (int i = 0; i < CECollider.edges.GetLength(0); i++)
|
||||
if (outHit)
|
||||
{
|
||||
//Debug.DrawLine(points[edges[i, 0]], points[edges[i, 1]], Color.red);
|
||||
|
||||
if (!Physics.Linecast(position + localPoints[edges[i, 0]] * skinWidth, position + localPoints[edges[i, 1]] * skinWidth, out outHit, mask, QueryTriggerInteraction.Ignore))
|
||||
{
|
||||
if (!Physics.Linecast(position + localPoints[edges[i, 1]] * skinWidth, position + localPoints[edges[i, 0]] * skinWidth, out outHit, mask, QueryTriggerInteraction.Ignore))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
///RETURN DISTANCE FROM CENTER
|
||||
return outHit;
|
||||
CECollider agent = new CECollider((BoxCollider2D)outHit.collider);
|
||||
CECollider myAgent = new CEVirtualCollider(position, collider);
|
||||
collision = CECollision.ResolveCollision(myAgent, agent);
|
||||
}
|
||||
|
||||
///IF REACH HERE DIDNT FOUND SHIT
|
||||
return outHit;
|
||||
return collision;
|
||||
}
|
||||
}
|
||||
|
||||
public class CEVirtualCollider : CECollider
|
||||
{
|
||||
protected Vector2 virtualPosition;
|
||||
public override Vector2 position
|
||||
{
|
||||
get
|
||||
{
|
||||
return virtualPosition;
|
||||
}
|
||||
}
|
||||
|
||||
public CEVirtualCollider (Vector2 pos, BoxCollider2D coll) : base(coll)
|
||||
{
|
||||
virtualPosition = pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace CharacterEngine.Primitives
|
||||
{
|
||||
public struct CECollision
|
||||
{
|
||||
public Vector2 impulse;
|
||||
public Vector2 point;
|
||||
public Vector2 normal;
|
||||
|
||||
/// <summary>
|
||||
/// RESOLVE THE COLLISION AND RETURN A COLLISION VALUE
|
||||
/// a = Collider wich is dynamic
|
||||
/// b = static Collider
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
public static CECollision ResolveCollision (CECollider a, CECollider b)
|
||||
{
|
||||
Vector2 closestPoint = Vector2.zero;
|
||||
CECollision collision = new CECollision();
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
Vector2 currentPoint = a.GetPoint(i);
|
||||
if (b.ContainsPoint(currentPoint))
|
||||
{
|
||||
closestPoint = ClosestPointInBounds(b, currentPoint);
|
||||
|
||||
collision.impulse = closestPoint - currentPoint;
|
||||
collision.normal = collision.impulse.normalized;
|
||||
collision.point = closestPoint;
|
||||
|
||||
return collision;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
Vector2 currentPoint = b.GetPoint(i);
|
||||
if (a.ContainsPoint(currentPoint))
|
||||
{
|
||||
closestPoint = ClosestPointInBounds(a, currentPoint);
|
||||
|
||||
collision.impulse = (closestPoint - currentPoint) * -1;
|
||||
collision.normal = collision.impulse.normalized;
|
||||
collision.point = closestPoint;
|
||||
|
||||
return collision;
|
||||
}
|
||||
}
|
||||
|
||||
//IF REACH HERE DIDNT RESOLVE SHIT
|
||||
Debug.Log("Could not resolve collision");
|
||||
return collision;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// GET CLOSEST CONTACT POINT
|
||||
/// </summary>
|
||||
/// <param name="coll"></param>
|
||||
/// <param name="point"></param>
|
||||
/// <returns></returns>
|
||||
public static Vector2 ClosestPointInBounds (CECollider coll, Vector2 point)
|
||||
{
|
||||
Vector2 smallerDelta = Vector2.zero;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
Vector2 cpis = ClosestVectorToSegment(point, coll.GetPoint(CECollider.edges[i, 0]), coll.GetPoint(CECollider.edges[i, 1]));
|
||||
Vector2 delta = cpis - point;
|
||||
|
||||
if (smallerDelta == Vector2.zero) smallerDelta = delta;
|
||||
else
|
||||
{
|
||||
if (delta.sqrMagnitude < smallerDelta.sqrMagnitude)
|
||||
smallerDelta = delta;
|
||||
}
|
||||
}
|
||||
|
||||
return point + smallerDelta;
|
||||
}
|
||||
|
||||
public static Vector2 ClosestVectorToSegment (Vector2 p, Vector2 a, Vector2 b)
|
||||
{
|
||||
Vector2 segmentNormal = Vector3.Cross((a- b).normalized, Vector3.forward);
|
||||
Vector2 distanceToPointA = a - p;
|
||||
Vector2 distanceToPointB = b - p;
|
||||
|
||||
return p + (Vector2)Vector3.Project(distanceToPointA, segmentNormal);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 348109b22797c724282c6d7807b00b73
|
||||
timeCreated: 1463330859
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -24,8 +24,8 @@ namespace CharacterEngine.Primitives
|
||||
private CEController controller;
|
||||
public readonly CECollider collider;
|
||||
|
||||
private Vector3 velocity;
|
||||
public Vector3 Velocity
|
||||
private Vector2 velocity;
|
||||
public Vector2 Velocity
|
||||
{
|
||||
get { return velocity; }
|
||||
}
|
||||
@@ -42,7 +42,7 @@ namespace CharacterEngine.Primitives
|
||||
public CEPrimitives (CEController controller)
|
||||
{
|
||||
this.controller = controller;
|
||||
collider = new CECollider(controller.GetComponent<Collider>());
|
||||
collider = new CECollider(controller.GetComponent<BoxCollider2D>());
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -89,20 +89,22 @@ namespace CharacterEngine.Primitives
|
||||
|
||||
bool CollideAndSlide()
|
||||
{
|
||||
Vector3 oldPos = transform.position;
|
||||
Vector3 predictedPos = oldPos + velocity * Time.fixedDeltaTime;
|
||||
Vector3 correctedPos = predictedPos;
|
||||
|
||||
RaycastHit hit = collider.SweepTest(velocity, velocity.magnitude * Time.fixedDeltaTime, controller.skinWidth);
|
||||
Vector2 oldPos = transform.position;
|
||||
Vector2 predictedPos = oldPos + velocity * Time.fixedDeltaTime;
|
||||
Vector2 correctedPos = predictedPos;
|
||||
|
||||
if (hit.collider == null) { return false; }
|
||||
CECollision hit = collider.SweepTest(velocity, velocity.magnitude * Time.fixedDeltaTime);
|
||||
|
||||
Vector3 obstacleHitPoint = hit.point;
|
||||
Vector3 obstacleNormal = hit.normal;
|
||||
Vector3 obstacleTangent = Vector3.Cross(obstacleNormal, Vector3.forward);
|
||||
if (hit.normal == Vector2.zero) { return false; }
|
||||
|
||||
Vector2 obstacleHitPoint = hit.point;
|
||||
Vector2 obstacleNormal = hit.normal;
|
||||
Vector2 obstacleTangent = Vector3.Cross(obstacleNormal, Vector3.forward);
|
||||
if (obstacleTangent.magnitude == 0)
|
||||
obstacleTangent = Vector3.Cross(obstacleNormal, Vector3.up);
|
||||
|
||||
Debug.DrawRay(hit.point, hit.normal);
|
||||
/*
|
||||
Plane obstaclePlane = new Plane(obstacleNormal, obstacleHitPoint);
|
||||
Ray velRay = new Ray(oldPos, velocity);
|
||||
@@ -112,12 +114,11 @@ namespace CharacterEngine.Primitives
|
||||
Vector3 vectorFromCenter = obstacleHitPoint - Coll.bounds.center ;
|
||||
*/
|
||||
|
||||
correctedPos = oldPos + velocity.normalized * hit.distance;
|
||||
|
||||
/*
|
||||
GameObject a = Instantiate(this.gameObject, correctedPos, transform.rotation) as GameObject;
|
||||
a.GetComponent<EntityController>().enabled = false;
|
||||
*/
|
||||
correctedPos = predictedPos + hit.impulse;
|
||||
|
||||
//GameObject a = CEController.Instantiate(controller.gameObject, correctedPos, transform.rotation) as GameObject;
|
||||
//a.GetComponent<CEController>().enabled = false;
|
||||
|
||||
|
||||
Vector3 epsilon = correctedPos - predictedPos;
|
||||
|
||||
@@ -129,9 +130,7 @@ namespace CharacterEngine.Primitives
|
||||
*/
|
||||
|
||||
Vector3 projectedForce = Vector3.Project(velocity, obstacleTangent);
|
||||
|
||||
velocity = projectedForce;
|
||||
velocity.z = 0.0f;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -143,7 +142,7 @@ namespace CharacterEngine.Primitives
|
||||
|
||||
void Move()
|
||||
{
|
||||
transform.position += velocity * Time.deltaTime;
|
||||
transform.position += (Vector3)velocity * Time.deltaTime;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
22
New Unity Project/Assets/Scripts/CharacterController/test.cs
Normal file
22
New Unity Project/Assets/Scripts/CharacterController/test.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using CharacterEngine.Primitives;
|
||||
|
||||
public class test : MonoBehaviour {
|
||||
|
||||
public BoxCollider2D coll;
|
||||
protected CECollider ce;
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
ce = new CECollider(coll);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
if (ce.ContainsPoint(transform.position))
|
||||
{
|
||||
Vector2 p = CECollision.ClosestPointInBounds(ce, transform.position);
|
||||
Debug.DrawLine(p, transform.position, Color.red);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f0ed370640e457f4ca5447af56a3319c
|
||||
timeCreated: 1463341313
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
9
New Unity Project/Assets/Scripts/Forces.meta
Normal file
9
New Unity Project/Assets/Scripts/Forces.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 713d19245f9f01349b61eade4b876d9b
|
||||
folderAsset: yes
|
||||
timeCreated: 1463261964
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
204
New Unity Project/Assets/Scripts/Forces/Force.cs
Normal file
204
New Unity Project/Assets/Scripts/Forces/Force.cs
Normal file
@@ -0,0 +1,204 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
namespace Forces
|
||||
{
|
||||
public class Force : System.Object
|
||||
{
|
||||
|
||||
public ForceProps properties;
|
||||
|
||||
protected Vector2 goal_vector = Vector2.up;
|
||||
protected Vector2 last_velocity = Vector2.zero;
|
||||
protected Vector2 init_in = Vector2.zero;
|
||||
protected Vector2 init_out = Vector2.zero;
|
||||
protected Vector2 current_output = Vector2.zero;
|
||||
protected float init_time = 0.0f;
|
||||
|
||||
/// <summary>
|
||||
/// last call from velocity method
|
||||
/// </summary>
|
||||
protected float last_step_time = 0.0f;
|
||||
/// <summary>
|
||||
/// last call from user (like AddForce) to now when start fading out
|
||||
/// </summary>
|
||||
protected float last_call_time = 0.0f;
|
||||
|
||||
protected float current_delta_step = 0.0f;
|
||||
protected float current_delta_call = 0.0f;
|
||||
protected float current_delta_notCall = 0.0f;
|
||||
|
||||
/// <summary>
|
||||
/// time since init calling
|
||||
/// </summary>
|
||||
protected float time_calling = 0.0f;
|
||||
/// <summary>
|
||||
/// absolute time since init
|
||||
/// </summary>
|
||||
protected float time_stepping = 0.0f;
|
||||
/// <summary>
|
||||
/// time since left calling
|
||||
/// </summary>
|
||||
protected float time_notCalling = 0.0f;
|
||||
|
||||
protected bool killed = false;
|
||||
|
||||
public Force(string id, Vector2 target_direction, float target_speed, float inertia_in = 1.0f, float inertia_out = 1.0f, float impulse = 0.0f)
|
||||
{
|
||||
properties = new ForceProps(id, target_speed, inertia_in, inertia_out, impulse);
|
||||
|
||||
Call(target_direction);
|
||||
}
|
||||
public Force(ForceProps props, Vector2 target_direction)
|
||||
{
|
||||
properties = props;
|
||||
|
||||
Call(target_direction);
|
||||
}
|
||||
public Vector2 Velocity()
|
||||
{
|
||||
Vector2 delta = Vector2.zero;
|
||||
Vector2 current_target_velocity = Vector2.zero;
|
||||
|
||||
if (init_time == 0.0f) Initialize();
|
||||
|
||||
StartStep();
|
||||
|
||||
current_target_velocity = FadeIn() + FadeOut();
|
||||
delta = current_target_velocity;
|
||||
current_output = Clamp(last_velocity + delta, goal_vector);
|
||||
|
||||
EndStep();
|
||||
|
||||
return current_output;
|
||||
}
|
||||
/// <summary>
|
||||
/// called by user to add force
|
||||
/// </summary>
|
||||
public void Call(Vector2 new_goal_vector)
|
||||
{
|
||||
if (properties.forceMode == ForceMode.Impulse)
|
||||
Initialize();
|
||||
|
||||
last_call_time = Time.time;
|
||||
goal_vector = new_goal_vector.normalized * properties.target_speed;
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines if the force is being used after reaching zero force
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool Using()
|
||||
{
|
||||
//Wait half a second to check if the velocity is null
|
||||
return ((Time.time - last_call_time) < 0.5f || last_velocity != Vector2.zero) && !killed;
|
||||
}
|
||||
public void Kill ()
|
||||
{
|
||||
last_call_time = 0.0f;
|
||||
current_delta_call = 0.0f;
|
||||
current_delta_notCall = 0.0f;
|
||||
last_velocity = Vector2.zero;
|
||||
killed = true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns the value of the last call
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Vector2 LastVelocity()
|
||||
{
|
||||
return last_velocity;
|
||||
}
|
||||
|
||||
protected void Initialize()
|
||||
{
|
||||
init_time = Time.time;
|
||||
}
|
||||
|
||||
protected void StartStep()
|
||||
{
|
||||
///Is the force acting like an impulse? if it is, then stop calling if impulse time < currentTime
|
||||
bool impulse = (properties.impulse > 0.0f && Time.time - init_time < properties.impulse);
|
||||
|
||||
current_delta_step = last_step_time == 0.0f ? 0.0f : Time.time - last_step_time;
|
||||
|
||||
if (last_call_time == 0.0f)
|
||||
{
|
||||
current_delta_call = 0.0f;
|
||||
current_delta_notCall = 0.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (last_step_time - last_call_time > current_delta_step && !impulse)
|
||||
{
|
||||
current_delta_notCall = current_delta_step;
|
||||
///set a start point to fade velocity
|
||||
init_out = last_velocity;
|
||||
current_delta_call = 0.0f;
|
||||
time_calling = 0.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
current_delta_call = current_delta_step;
|
||||
///set a start point to fade velocity
|
||||
init_in = last_velocity;
|
||||
current_delta_notCall = 0.0f;
|
||||
time_notCalling = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
time_calling += current_delta_call;
|
||||
time_stepping += current_delta_step;
|
||||
}
|
||||
|
||||
protected Vector2 FadeIn()
|
||||
{
|
||||
// float fade_in_fact = 0.0f;
|
||||
Vector2 output = Vector2.zero;
|
||||
|
||||
if (current_delta_call <= 0.0f) return Vector2.zero;
|
||||
|
||||
//fade_in_fact = Mathf.Clamp01(time_calling / inertia_in);
|
||||
output = (goal_vector - last_velocity) * (current_delta_call / properties.inertia_in);
|
||||
|
||||
return output;
|
||||
}
|
||||
protected Vector2 FadeOut()
|
||||
{
|
||||
//float fade_out_fact = 0.0f;
|
||||
Vector2 output = Vector2.zero;
|
||||
|
||||
if (current_delta_notCall <= 0.0f) return Vector2.zero;
|
||||
|
||||
//fade_out_fact = Mathf.Clamp01(time_notCalling / inertia_in);
|
||||
output = (Vector2.zero - last_velocity) * (current_delta_notCall / properties.inertia_out);
|
||||
|
||||
return output;
|
||||
}
|
||||
protected Vector2 Clamp(Vector2 vector, Vector2 max)
|
||||
{
|
||||
float max_magnitude = max.magnitude;
|
||||
float currentMagnitude = vector.magnitude;
|
||||
float dot = Vector2.Dot(vector, max);
|
||||
Vector2 output = vector;
|
||||
|
||||
//check if it is calling to clamp
|
||||
if (currentMagnitude > max_magnitude && dot > 0.0f && current_delta_call != 0.0f)
|
||||
{
|
||||
output = max;
|
||||
}
|
||||
//checl if is not calling to clamp to zero
|
||||
else if (dot <= 0.0f && current_delta_call == 0.0f)
|
||||
{
|
||||
output = Vector2.zero;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
protected void EndStep()
|
||||
{
|
||||
last_step_time = Time.time;
|
||||
last_velocity = current_output;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
12
New Unity Project/Assets/Scripts/Forces/Force.cs.meta
Normal file
12
New Unity Project/Assets/Scripts/Forces/Force.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57b436089801943408c989af783a9a85
|
||||
timeCreated: 1455409843
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
93
New Unity Project/Assets/Scripts/Forces/ForceController.cs
Normal file
93
New Unity Project/Assets/Scripts/Forces/ForceController.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Forces
|
||||
{
|
||||
/// <summary>
|
||||
/// Controls forces to be esay for the user to use
|
||||
/// </summary>
|
||||
public class ForceController : System.Object
|
||||
{
|
||||
protected Dictionary<string, Force> active_forces = new Dictionary<string, Force>();
|
||||
protected List<Force2Call> call_forces = new List<Force2Call>();
|
||||
protected List<string> null_forces_index = new List<string>();
|
||||
|
||||
protected Vector2 lastFinalVel;
|
||||
|
||||
public void AddForce(ForceProps forceProps, Vector3 direction)
|
||||
{
|
||||
Force target = Target(forceProps, direction);
|
||||
call_forces.Add(new Force2Call(direction, target));
|
||||
}
|
||||
|
||||
public void DestroyForce (ForceProps forceProps)
|
||||
{
|
||||
Force f = GetForce(forceProps);
|
||||
if (f == null) return;
|
||||
f.Kill();
|
||||
null_forces_index.Add(forceProps.Name);
|
||||
}
|
||||
|
||||
public Force GetForce(ForceProps force)
|
||||
{
|
||||
if (active_forces.ContainsKey(force.Name))
|
||||
return active_forces[force.Name];
|
||||
return null;
|
||||
}
|
||||
|
||||
public Vector2 Update()
|
||||
{
|
||||
Vector2 final_velocity = Vector2.zero;
|
||||
|
||||
///ADD AND CALL STEP
|
||||
foreach (Force2Call f in call_forces)
|
||||
{
|
||||
f.force.Call(f.direction);
|
||||
}
|
||||
|
||||
///APPLY STEP
|
||||
foreach (string key in active_forces.Keys)
|
||||
{
|
||||
Force f = active_forces[key];
|
||||
|
||||
///Add to null forces if the force isnt using
|
||||
if (!f.Using())
|
||||
{
|
||||
null_forces_index.Add(f.properties.Name);
|
||||
continue;
|
||||
}
|
||||
|
||||
Vector2 add = f.Velocity();
|
||||
final_velocity += add;
|
||||
}
|
||||
|
||||
///CLEAR STEP
|
||||
foreach (string i in null_forces_index)
|
||||
{
|
||||
active_forces.Remove(i);
|
||||
}
|
||||
|
||||
call_forces.Clear();
|
||||
null_forces_index.Clear();
|
||||
|
||||
Vector2 final = final_velocity * Time.deltaTime * 100.0f;
|
||||
Vector3 out_ = final - lastFinalVel;
|
||||
lastFinalVel = final;
|
||||
|
||||
return out_;
|
||||
}
|
||||
|
||||
protected Force Target(ForceProps force, Vector3 direction)
|
||||
{
|
||||
Force out_ = GetForce(force);
|
||||
|
||||
if (out_ == null)
|
||||
{
|
||||
out_ = new Force(force, direction);
|
||||
active_forces.Add(out_.properties.Name, out_);
|
||||
}
|
||||
|
||||
return out_;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 114545d266452cb4b96fd2ef86902650
|
||||
timeCreated: 1456794068
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
63
New Unity Project/Assets/Scripts/Forces/ForceStruct.cs
Normal file
63
New Unity Project/Assets/Scripts/Forces/ForceStruct.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace Forces
|
||||
{
|
||||
public enum ForceMode { Force, Impulse, Aceleration }
|
||||
/// <summary>
|
||||
/// Label force static information
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public struct ForceProps
|
||||
{
|
||||
/// <summary>
|
||||
/// indetify instance id
|
||||
/// </summary>
|
||||
public string Name;
|
||||
/// <summary>
|
||||
/// inertia value when starts in seconds (fade in)
|
||||
/// </summary>
|
||||
public float inertia_in;
|
||||
/// <summary>
|
||||
/// inertia value when ends in seconds (fade out)
|
||||
/// </summary>
|
||||
public float inertia_out;
|
||||
/// <summary>
|
||||
/// time calling without Update
|
||||
/// </summary>
|
||||
public float impulse;
|
||||
/// <summary>
|
||||
/// goal speed
|
||||
/// </summary>
|
||||
public float target_speed;
|
||||
/// <summary>
|
||||
/// mode of applying this force
|
||||
/// </summary>
|
||||
public ForceMode forceMode;
|
||||
|
||||
|
||||
public ForceProps(string Name, float target_speed, float inertia_in = 1.0f, float inertia_out = 1.0f, float impulse = 0.0f)
|
||||
{
|
||||
this.Name = Name;
|
||||
this.target_speed = target_speed;
|
||||
this.inertia_in = inertia_in;
|
||||
this.inertia_out = inertia_out;
|
||||
this.impulse = impulse;
|
||||
this.forceMode = impulse > 0.0f ? ForceMode.Impulse : ForceMode.Force;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Target force and new vector to apply
|
||||
/// </summary>
|
||||
public struct Force2Call
|
||||
{
|
||||
public Vector3 direction;
|
||||
public Force force;
|
||||
|
||||
public Force2Call(Vector3 direction, Force force)
|
||||
{
|
||||
this.direction = direction;
|
||||
this.force = force;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
New Unity Project/Assets/Scripts/Forces/ForceStruct.cs.meta
Normal file
12
New Unity Project/Assets/Scripts/Forces/ForceStruct.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2f6cb937bd9326148b7ab27db4d1d775
|
||||
timeCreated: 1456794126
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
104
New Unity Project/Assets/Scripts/Player.cs
Normal file
104
New Unity Project/Assets/Scripts/Player.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using Forces;
|
||||
|
||||
public class Player : MonoBehaviour {
|
||||
|
||||
public float weight = 20.0f;
|
||||
|
||||
protected new Collider collider;
|
||||
protected Rigidbody rigidBody;
|
||||
protected Vector3 velocity;
|
||||
protected ForceController controller;
|
||||
|
||||
public ForceProps[] forces;
|
||||
|
||||
protected bool grounding;
|
||||
|
||||
void Awake ()
|
||||
{
|
||||
rigidBody = GetComponent<Rigidbody>();
|
||||
controller = new ForceController();
|
||||
collider = GetComponent<Collider>();
|
||||
}
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update ()
|
||||
{
|
||||
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.3f)
|
||||
{
|
||||
float value = Mathf.Sign(Input.GetAxis("Horizontal"));
|
||||
controller.AddForce(forces[0], Vector3.right * value);
|
||||
}
|
||||
if (Input.GetButtonDown("A") && grounding)
|
||||
{
|
||||
controller.AddForce(forces[1], Vector3.up);
|
||||
}
|
||||
}
|
||||
|
||||
void FixedUpdate ()
|
||||
{
|
||||
Step();
|
||||
}
|
||||
|
||||
void OnLanded ()
|
||||
{
|
||||
controller.DestroyForce(forces[1]);
|
||||
velocity.y = 0.0f;
|
||||
}
|
||||
|
||||
void Step ()
|
||||
{
|
||||
bool lastGrounded = grounding;
|
||||
|
||||
velocity = rigidBody.velocity;
|
||||
|
||||
velocity.y += -8.0f * weight * Time.fixedDeltaTime;
|
||||
|
||||
Vector2 forceControllerVel = controller.Update();
|
||||
velocity.x += forceControllerVel.x;
|
||||
|
||||
if (controller.GetForce(forces[1]) != null)
|
||||
{
|
||||
Debug.Log(forceControllerVel.y);
|
||||
}
|
||||
|
||||
velocity.y += forceControllerVel.y;
|
||||
|
||||
grounding = IsGrounded();
|
||||
|
||||
if (lastGrounded != grounding)
|
||||
{
|
||||
if (grounding) OnLanded();
|
||||
}
|
||||
|
||||
rigidBody.velocity = velocity;
|
||||
}
|
||||
|
||||
bool IsGrounded()
|
||||
{
|
||||
Vector3[] raycastPoints =
|
||||
{
|
||||
new Vector3(collider.bounds.max.x, collider.bounds.min.y + 0.1f, collider.bounds.center.z),
|
||||
new Vector3(collider.bounds.min.x, collider.bounds.min.y + 0.1f, collider.bounds.center.z),
|
||||
new Vector3(collider.bounds.center.x, collider.bounds.min.y + 0.1f, collider.bounds.center.z)
|
||||
};
|
||||
|
||||
for (int i = 0; i < raycastPoints.Length; i++)
|
||||
{
|
||||
//Debug.DrawRay(raycastPoints[i], Vector3.down * (0.1f + velocity.magnitude * Time.fixedDeltaTime));
|
||||
|
||||
if (Physics.Raycast(raycastPoints[i], Vector3.down, 0.1f + velocity.magnitude * Time.fixedDeltaTime))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
12
New Unity Project/Assets/Scripts/Player.cs.meta
Normal file
12
New Unity Project/Assets/Scripts/Player.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 16a8185af630ffa4fadff4fdb168e89a
|
||||
timeCreated: 1463245545
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user