COLLISION SHIT FINALLY WORKING WOOO

next:

-Find collision contact point.
This commit is contained in:
Marquitos
2016-05-16 18:56:43 -03:00
parent e7f07a6e53
commit f744a744b7
73 changed files with 274 additions and 505 deletions

Binary file not shown.

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3f7585d4a547a6e4191719736d0daf78
timeCreated: 1463417062
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,7 +1,7 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 713d19245f9f01349b61eade4b876d9b guid: 1ab5a05df27fed64291f5d2341f929b7
folderAsset: yes folderAsset: yes
timeCreated: 1463261964 timeCreated: 1463416934
licenseType: Free licenseType: Free
DefaultImporter: DefaultImporter:
userData: userData:

Binary file not shown.

View File

@@ -0,0 +1,78 @@
fileFormatVersion: 2
guid: 092e42aed3ea51d49b61711bd48284fe
timeCreated: 1463416939
licenseType: Free
ModelImporter:
serializedVersion: 19
fileIDToRecycleName:
100000: //RootNode
400000: //RootNode
2300000: //RootNode
3300000: //RootNode
4300000: Cube
7400000: Default Take
9500000: //RootNode
materials:
importMaterials: 0
materialName: 0
materialSearch: 1
animations:
legacyGenerateAnimations: 4
bakeSimulation: 0
resampleRotations: 1
optimizeGameObjects: 0
motionNodeName:
animationImportErrors:
animationImportWarnings:
animationRetargetingWarnings:
animationDoRetargetingWarnings: 0
animationCompression: 1
animationRotationError: 0.5
animationPositionError: 0.5
animationScaleError: 0.5
animationWrapMode: 0
extraExposedTransformPaths: []
clipAnimations: []
isReadable: 1
meshes:
lODScreenPercentages: []
globalScale: 1
meshCompression: 0
addColliders: 0
importBlendShapes: 1
swapUVChannels: 0
generateSecondaryUV: 0
useFileUnits: 1
optimizeMeshForGPU: 1
keepQuads: 0
weldVertices: 1
secondaryUVAngleDistortion: 8
secondaryUVAreaDistortion: 15.000001
secondaryUVHardAngle: 88
secondaryUVPackMargin: 4
useFileScale: 1
tangentSpace:
normalSmoothAngle: 60
normalImportMode: 0
tangentImportMode: 3
importAnimation: 0
copyAvatar: 0
humanDescription:
human: []
skeleton: []
armTwist: 0.5
foreArmTwist: 0.5
upperLegTwist: 0.5
legTwist: 0.5
armStretch: 0.05
legStretch: 0.05
feetSpacing: 0
rootMotionBoneName:
hasTranslationDoF: 0
lastHumanDescriptionAvatarSource: {instanceID: 0}
animationType: 0
humanoidOversampling: 1
additionalBone: 0
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: fead40d50297ab84baf2200692cb99a0
timeCreated: 1463417014
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: fa5061d9c09bf974c8583d5e72982fd9
folderAsset: yes
timeCreated: 1463416939
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e180352f7114a4b4fab550f64fd9271a
timeCreated: 1463416939
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -4,13 +4,50 @@ using System.Collections.Generic;
namespace CharacterEngine.Primitives namespace CharacterEngine.Primitives
{ {
public class CEProjection
{
public Vector2 axis;
public float max;
public float min;
public CEProjection() { }
public CEProjection (CECollider coll, Vector2 axis)
{
Project(coll, axis);
}
public void Project(CECollider coll, Vector2 axis)
{
for (int i = 0; i < 4; i++)
{
Vector2 p = Vector3.Project(coll.GetPoint(i), axis);
float distance = p.magnitude * Mathf.Sign(Vector2.Dot(p, axis));
if (i == 0)
{
max = min = distance;
}
else
{
if (distance > max) max = distance;
else if (distance < min) min = distance;
}
}
}
}
public class CECollider public class CECollider
{ {
public static int[,] edges = new int[,] public static int[,] edges = new int[,]
{ {
{0, 2}, {0, 3}, {1, 2},{1, 3} {0, 2}, {0, 3}, {1, 2},{1, 3}
}; };
public static int[,] pointsNeighbours = new int[,]
{
{2, 3}, {3, 2}, {0, 1},{1, 0}
};
private BoxCollider2D collider; private BoxCollider2D collider;
public BoxCollider2D Coll public BoxCollider2D Coll
{ {
@@ -70,22 +107,30 @@ namespace CharacterEngine.Primitives
/// </summary> /// </summary>
/// <param name="index"></param> /// <param name="index"></param>
/// <returns></returns> /// <returns></returns>
public virtual Vector2 GetPoint (int index) public Vector2 GetPoint (int index)
{ {
return position + (Vector2)(collider.transform.rotation * Vector2.Scale(localPoints[index], collider.transform.lossyScale)); return position + (Vector2)(collider.transform.rotation * Vector2.Scale(localPoints[index], collider.transform.lossyScale));
} }
public virtual Vector2 GetPoint(int index, Vector2 position) public Vector2 GetPoint(int index, Vector2 position)
{ {
return position + (Vector2)(collider.transform.rotation * Vector2.Scale(localPoints[index], collider.transform.lossyScale)); return position + (Vector2)(collider.transform.rotation * Vector2.Scale(localPoints[index], collider.transform.lossyScale));
} }
public virtual Vector2 GetAABBPoint(int index) public Vector2 GetAABBPoint(int index)
{ {
return position + (Vector2)(Vector2.Scale(localPoints[index], collider.transform.lossyScale)); return position + (Vector2)(Vector2.Scale(localPoints[index], collider.transform.lossyScale));
} }
public virtual Vector2 GetAABBPoint(int index, Vector2 position) public Vector2 GetAABBPoint(int index, Vector2 position)
{ {
return position + (Vector2)(Vector2.Scale(localPoints[index], collider.transform.lossyScale)); return position + (Vector2)(Vector2.Scale(localPoints[index], collider.transform.lossyScale));
} }
public Vector2 GetNormal(int index)
{
return Vector3.Cross(GetPoint(edges[index, 0]) - GetPoint(edges[index, 1]), Vector3.forward).normalized;
}
public Vector2 GetNormal(int index, Vector2 position)
{
return Vector3.Cross(GetPoint(edges[index, 0], position) - GetPoint(edges[index, 1], position), Vector3.forward).normalized;
}
public bool ContainsPoint (Vector2 p) public bool ContainsPoint (Vector2 p)
{ {
@@ -96,6 +141,11 @@ namespace CharacterEngine.Primitives
return insideX && insideY; return insideX && insideY;
} }
public void Project(Vector2 normal, Vector2 p)
{
}
/// <summary> /// <summary>
/// Collision test along a path /// Collision test along a path
/// </summary> /// </summary>

View File

@@ -20,6 +20,37 @@ namespace CharacterEngine.Primitives
{ {
Vector2 closestPoint = Vector2.zero; Vector2 closestPoint = Vector2.zero;
CECollision collision = new CECollision(); CECollision collision = new CECollision();
///IS NOT MTV THO (music television) is MINIMUN TRANSLATION VECTOR (funny joke uh)
Vector2 MTV = Vector2.zero;
float minimunOverlap = 0.0f;
Vector2[] axes = new Vector2[8];
for (int i = 0; i < 8; i++)
{
axes[i] = i < 4 ? a.GetNormal(i) : b.GetNormal(i - 4);
}
for (int i = 0; i < axes.Length; i++)
{
CEProjection projectionA = new CEProjection(a, axes[i]);
CEProjection projectionB = new CEProjection(b, axes[i]);
float overlapValue = Mathf.Abs(projectionA.max - projectionB.min);
if (i == 0 || overlapValue < minimunOverlap)
{
minimunOverlap = overlapValue;
MTV = axes[i] * minimunOverlap;
}
}
collision.impulse = MTV;
collision.normal = collision.impulse.normalized;
///COULDNT DETRMINE YET HOW TO FIND POINT ILL DO IT ASFASDGSD
collision.point = Vector2.zero;
/*
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
Vector2 currentPoint = a.GetPoint(i); Vector2 currentPoint = a.GetPoint(i);
@@ -49,9 +80,10 @@ namespace CharacterEngine.Primitives
return collision; return collision;
} }
} }
*/
//IF REACH HERE DIDNT RESOLVE SHIT //IF REACH HERE DIDNT RESOLVE SHIT
Debug.Log("Could not resolve collision"); //Debug.Log("Could not resolve collision");
return collision; return collision;
} }
@@ -88,6 +120,12 @@ namespace CharacterEngine.Primitives
return p + (Vector2)Vector3.Project(distanceToPointA, segmentNormal); return p + (Vector2)Vector3.Project(distanceToPointA, segmentNormal);
} }
public static Vector2 ClosestVectorToPlane(Vector2 p, Vector2 normal, Vector2 position)
{
Vector2 plane = Vector3.Cross(normal, Vector3.forward);
Vector2 distanceToPosition = position - p;
return p + (Vector2)Vector3.Project(distanceToPosition, plane);
}
} }
} }

View File

@@ -17,6 +17,8 @@ namespace CharacterEngine.Controller
#endregion #endregion
protected Vector2 moveVelocity;
private CEPrimitives primitives; private CEPrimitives primitives;
/*Summary: /*Summary:
@@ -30,12 +32,6 @@ namespace CharacterEngine.Controller
primitives = new CEPrimitives(this); primitives = new CEPrimitives(this);
} }
// Use this for initialization
void Start()
{
}
/*Summary: /*Summary:
In Updates, we have both functions native from unity Monobehaviour: Update & FixedUpdate. In Updates, we have both functions native from unity Monobehaviour: Update & FixedUpdate.
The difference of this both is that one (Update) excecute every frame, and the other one, is executed The difference of this both is that one (Update) excecute every frame, and the other one, is executed
@@ -43,15 +39,20 @@ namespace CharacterEngine.Controller
while "Fixed Update" dont. while "Fixed Update" dont.
*/ */
// Update is called once per frame
void Update()
{
}
void FixedUpdate () void FixedUpdate ()
{ {
MoveStep();
primitives.Step(); primitives.Step();
} }
void MoveStep ()
{
primitives.AddVelocity(moveVelocity);
}
public void Move (Vector2 velocity)
{
moveVelocity = velocity;
}
} }
} }

View File

@@ -59,6 +59,11 @@ namespace CharacterEngine.Primitives
PhysicsStep(); PhysicsStep();
} }
public void AddVelocity ( Vector2 velocity )
{
this.velocity += velocity;
}
void PhysicsStep() void PhysicsStep()
{ {
GetVelocity(); GetVelocity();
@@ -78,7 +83,7 @@ namespace CharacterEngine.Primitives
void GravityStep() void GravityStep()
{ {
velocity.y += gravity * controller.weight * Time.deltaTime * 1.2f; velocity.y += gravity * controller.weight * Time.deltaTime * 1.2f;
velocity.x = -gravity * 0.5f; velocity.x = 10.0f;
} }
void CollisionStep() void CollisionStep()

View File

@@ -2,21 +2,34 @@
using System.Collections; using System.Collections;
using CharacterEngine.Primitives; using CharacterEngine.Primitives;
public class test : MonoBehaviour { public class Test : MonoBehaviour {
public BoxCollider2D coll; public Transform a;
protected CECollider ce; public Transform b;
// Use this for initialization public BoxCollider2D c;
void Start () { public BoxCollider2D c1;
ce = new CECollider(coll); public BoxCollider2D c2;
}
private CECollider cec;
// Update is called once per frame private CECollider cec1;
void Update () { private CECollider cec2;
if (ce.ContainsPoint(transform.position)) // Use this for initialization
{ void Start () {
Vector2 p = CECollision.ClosestPointInBounds(ce, transform.position); cec = new CECollider(c);
Debug.DrawLine(p, transform.position, Color.red); cec1 = new CECollider(c1);
} cec2 = new CECollider(c2);
}
}
// Update is called once per frame
void Update () {
Vector2 axis = (a.position - b.position).normalized;
CEProjection p = new CEProjection(cec, axis);
CEProjection p1 = new CEProjection(cec1, axis);
CEProjection p2 = new CEProjection(cec2, axis);
Debug.DrawLine(axis * p.min, axis * p.max, Color.red);
Debug.DrawLine(axis * p1.min, axis * p1.max, Color.blue);
Debug.DrawLine(axis * p2.min, axis * p2.max, Color.yellow);
}
} }

View File

@@ -1,6 +1,6 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: f0ed370640e457f4ca5447af56a3319c guid: 7a07c3efc56913144be618cb56ec5107
timeCreated: 1463341313 timeCreated: 1463429200
licenseType: Free licenseType: Free
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2

View File

@@ -1,204 +0,0 @@
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;
}
}
}

View File

@@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: 57b436089801943408c989af783a9a85
timeCreated: 1455409843
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,93 +0,0 @@
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_;
}
}
}

View File

@@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: 114545d266452cb4b96fd2ef86902650
timeCreated: 1456794068
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,63 +0,0 @@
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;
}
}
}

View File

@@ -1,12 +0,0 @@
fileFormatVersion: 2
guid: 2f6cb937bd9326148b7ab27db4d1d775
timeCreated: 1456794126
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,25 +1,15 @@
using UnityEngine; using UnityEngine;
using System.Collections; using System.Collections;
using Forces; using CharacterEngine.Controller;
public class Player : MonoBehaviour { public class Player : MonoBehaviour {
public float weight = 20.0f; protected CEController controller;
protected new Collider collider;
protected Rigidbody rigidBody;
protected Vector3 velocity;
protected ForceController controller;
public ForceProps[] forces;
protected bool grounding; protected bool grounding;
void Awake () void Awake ()
{ {
rigidBody = GetComponent<Rigidbody>(); controller = GetComponent<CEController>();
controller = new ForceController();
collider = GetComponent<Collider>();
} }
// Use this for initialization // Use this for initialization
void Start () { void Start () {
@@ -32,73 +22,22 @@ public class Player : MonoBehaviour {
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.3f) if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.3f)
{ {
float value = Mathf.Sign(Input.GetAxis("Horizontal")); float value = Mathf.Sign(Input.GetAxis("Horizontal"));
controller.AddForce(forces[0], Vector3.right * value); controller.Move(Vector3.right * value);
} }
/*
if (Input.GetButtonDown("A") && grounding) if (Input.GetButtonDown("A") && grounding)
{ {
controller.AddForce(forces[1], Vector3.up); //controller.AddForce(forces[1], Vector3.up);
} }
} */
void FixedUpdate ()
{
Step();
} }
void OnLanded () void OnLanded ()
{ {
/*
controller.DestroyForce(forces[1]); controller.DestroyForce(forces[1]);
velocity.y = 0.0f; 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;
} }
} }

Binary file not shown.

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 60d552ec687c4b145a91814cb030cb35
timeCreated: 1463417594
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB