Commit e8f95484 by Tim Reiter

in arbeit: den anderen player wegschubsen.

parent 9d58515b
fileFormatVersion: 2
guid: 70ff1f0f7ced823478a0d220dbf33c18
timeCreated: 1433438635
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
...@@ -218,6 +218,9 @@ public class CharacterController2D : MonoBehaviour ...@@ -218,6 +218,9 @@ public class CharacterController2D : MonoBehaviour
#endregion #endregion
private Vector3 lastPos; // we make sure the last Pos is never the curPos.
private Vector2 moveDirection;
[System.Diagnostics.Conditional( "DEBUG_CC2D_RAYS" )] [System.Diagnostics.Conditional( "DEBUG_CC2D_RAYS" )]
private void DrawRay( Vector3 start, Vector3 dir, Color color ) private void DrawRay( Vector3 start, Vector3 dir, Color color )
{ {
...@@ -227,6 +230,11 @@ public class CharacterController2D : MonoBehaviour ...@@ -227,6 +230,11 @@ public class CharacterController2D : MonoBehaviour
#region Public #region Public
public Vector2 getDirectionOfMovement()
{
return moveDirection.normalized;
}
/// <summary> /// <summary>
/// attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to /// attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to
/// stop when run into. /// stop when run into.
...@@ -260,6 +268,9 @@ public class CharacterController2D : MonoBehaviour ...@@ -260,6 +268,9 @@ public class CharacterController2D : MonoBehaviour
moveVertically( ref deltaMovement ); moveVertically( ref deltaMovement );
//Update last position:
lastPos = transform.position;
// move then update our state // move then update our state
if( usePhysicsForMovement ) if( usePhysicsForMovement )
{ {
...@@ -268,13 +279,15 @@ public class CharacterController2D : MonoBehaviour ...@@ -268,13 +279,15 @@ public class CharacterController2D : MonoBehaviour
} }
else else
{ {
transform.Translate( deltaMovement, Space.World ); transform.Translate(deltaMovement, Space.World);
// only calculate velocity if we have a non-zero deltaTime // only calculate velocity if we have a non-zero deltaTime
if( Time.deltaTime > 0 ) if( Time.deltaTime > 0 )
velocity = deltaMovement / Time.deltaTime; velocity = deltaMovement / Time.deltaTime;
} }
//update move direction after the currentposition was updated:
moveDirection = transform.position - lastPos;
// set our becameGrounded state based on the previous and current collision state // set our becameGrounded state based on the previous and current collision state
if( !collisionState.wasGroundedLastFrame && collisionState.below ) if( !collisionState.wasGroundedLastFrame && collisionState.below )
collisionState.becameGroundedThisFrame = true; collisionState.becameGroundedThisFrame = true;
......
...@@ -6,6 +6,7 @@ using System.Collections; ...@@ -6,6 +6,7 @@ using System.Collections;
public class PlayerControl : MonoBehaviour public class PlayerControl : MonoBehaviour
{ {
// movement config // movement config
public float pushForce = 20f;
public float gravity = -15f; public float gravity = -15f;
public float runSpeed = 8f; public float runSpeed = 8f;
public float groundDamping = 20f; // how fast do we change direction? higher means faster public float groundDamping = 20f; // how fast do we change direction? higher means faster
...@@ -28,9 +29,11 @@ public class PlayerControl : MonoBehaviour ...@@ -28,9 +29,11 @@ public class PlayerControl : MonoBehaviour
public int playerNumber = 1; //gibt an, ob es sich um player one, player two, etc. handelt. sollte nicht 0 sein; public int playerNumber = 1; //gibt an, ob es sich um player one, player two, etc. handelt. sollte nicht 0 sein;
KeyCode goRightKeyCode, goLeftKeyCode, jumpKeyCode, dashKeyCode; KeyCode goRightKeyCode, goLeftKeyCode, jumpKeyCode, dashKeyCode;
private Rigidbody2D rigidbody;
void Start() void Start()
{ {
rigidbody = GetComponent<Rigidbody2D>();
_controller = GetComponent<CharacterController2D>(); _controller = GetComponent<CharacterController2D>();
_controller.onControllerCollidedEvent += onControllerCollider; _controller.onControllerCollidedEvent += onControllerCollider;
...@@ -59,9 +62,17 @@ public class PlayerControl : MonoBehaviour ...@@ -59,9 +62,17 @@ public class PlayerControl : MonoBehaviour
void onControllerCollider(RaycastHit2D hit) void onControllerCollider(RaycastHit2D hit)
{ {
// bail out on plain old ground hits //// bail out on plain old ground hits
if (hit.normal.y == 1f) //if (hit.normal.y == 1f)
return; // return;
Rigidbody2D body = hit.collider.gameObject.GetComponent<Rigidbody2D>();
if (body)
{
//TODO abfrage " is dashing"
body.AddForce(_controller.getDirectionOfMovement()*pushForce);
//TODO set is dashing to false
}
// logs any collider hits // logs any collider hits
//Debug.Log( "flags: " + _controller.collisionState + ", hit.normal: " + hit.normal ); //Debug.Log( "flags: " + _controller.collisionState + ", hit.normal: " + hit.normal );
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment