Commit 5ee91849 by Tim Reiter

CharacterController2D deleted, own move system implemented in PlayerControl.

parent 46dad222
fileFormatVersion: 2
guid: 75ab554038a08ae479d95e7be9fcd332
timeCreated: 1433442334
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: e937fe68fce80d44fb385a099114c29f
timeCreated: 1433439077
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
...@@ -7,12 +7,21 @@ public class PlayerControl : MonoBehaviour ...@@ -7,12 +7,21 @@ public class PlayerControl : MonoBehaviour
{ {
private enum Keys : int { Left = 0, Right, Up, Down, Jump, Dash, Shoot }; private enum Keys : int { Left = 0, Right, Up, Down, Jump, Dash, Shoot };
private Rigidbody2D body2D;
private Vector3 lastPos; // we make sure the last Pos is never the curPos.
private Vector2 moveDirection;
public Transform groundCheck;
public LayerMask mask;
// movement config // movement config
public float pushForce = 20f; 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
public float inAirDamping = 5f; public float inAirDamping = 5f; //only horizontal
public float inAirDampingVertical = 10f;
public float targetJumpHeight = 2f; public float targetJumpHeight = 2f;
public float lastDashStart = float.NegativeInfinity; public float lastDashStart = float.NegativeInfinity;
[HideInInspector] [HideInInspector]
...@@ -33,9 +42,6 @@ public class PlayerControl : MonoBehaviour ...@@ -33,9 +42,6 @@ public class PlayerControl : MonoBehaviour
//[HideInInspector] //[HideInInspector]
public float normalizedHorizontalSpeed = 0; public float normalizedHorizontalSpeed = 0;
CharacterController2D _controller;
public RaycastHit2D lastControllerColliderHit;
[HideInInspector] [HideInInspector]
public Vector3 velocity; public Vector3 velocity;
...@@ -44,8 +50,7 @@ public class PlayerControl : MonoBehaviour ...@@ -44,8 +50,7 @@ public class PlayerControl : MonoBehaviour
void Start() void Start()
{ {
_controller = GetComponent<CharacterController2D>(); body2D = GetComponent<Rigidbody2D>();
_controller.onControllerCollidedEvent += onControllerCollider;
switch (playerNumber) switch (playerNumber)
{ {
...@@ -81,18 +86,19 @@ public class PlayerControl : MonoBehaviour ...@@ -81,18 +86,19 @@ public class PlayerControl : MonoBehaviour
if (body) if (body)
{ {
//TODO abfrage " is dashing" //TODO abfrage " is dashing"
body.AddForce(_controller.getDirectionOfMovement() * pushForce); body.AddForce(moveDirection * pushForce);
lastDashStart = Time.time - dashCompletionTime; lastDashStart = Time.time - dashCompletionTime;
currentDashCoolDown = dashCollisionCoolDown; currentDashCoolDown = dashCollisionCoolDown;
} }
} }
void Update() void FixedUpdate()
{ {
// grab our current velocity to use as a base for all calculations //Update last position:
velocity = _controller.velocity; lastPos = transform.position;
if (_controller.isGrounded) if (isGrounded())
velocity.y = 0; velocity.y = 0;
updateDashDirection(); updateDashDirection();
...@@ -110,45 +116,45 @@ public class PlayerControl : MonoBehaviour ...@@ -110,45 +116,45 @@ public class PlayerControl : MonoBehaviour
} }
else else
{ {
velocity = body2D.velocity;
if (Input.GetKey(keyCodes[(int)Keys.Right])) if (Input.GetKey(keyCodes[(int)Keys.Right]))
{ {
normalizedHorizontalSpeed = 1; //normalizedHorizontalSpeed = 1;
if (transform.localScale.x < 0f) //if (transform.localScale.x < 0f)
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z); // transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
//if (_controller.isGrounded)
} //go right:
else if (Input.GetKey(keyCodes[(int)Keys.Left])) velocity = new Vector2(runSpeed,velocity.y);
{
normalizedHorizontalSpeed = -1;
if (transform.localScale.x > 0f)
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
//if (_controller.isGrounded) //if (_controller.isGrounded)
} }
else else if (Input.GetKey(keyCodes[(int)Keys.Left]))
{ {
normalizedHorizontalSpeed = 0; //normalizedHorizontalSpeed = -1;
//if (transform.localScale.x > 0f)
// transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
velocity = new Vector2(-runSpeed, velocity.y);
//if (_controller.isGrounded) //if (_controller.isGrounded)
} }
if (Input.GetKey (keyCodes[(int)Keys.Jump])) if (Input.GetKey (keyCodes[(int)Keys.Jump]))
{ {
//to avoid DOUBLE JUMP //to avoid DOUBLE JUMP
if (_controller.isGrounded) if (isGrounded())
{ {
velocity.y = Mathf.Sqrt(2f * targetJumpHeight * -gravity); velocity = new Vector2(velocity.x, targetJumpHeight);
} }
} }
// apply horizontal speed smoothing it // apply horizontal speed smoothing it
var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction? var smoothedMovementFactor = isGrounded() ? groundDamping : inAirDamping; // how fast do we change direction?
velocity.x = Mathf.Lerp(velocity.x, normalizedHorizontalSpeed * rawMovementDirection * runSpeed, Time.deltaTime * smoothedMovementFactor); velocity.x = Mathf.Lerp(velocity.x,0, Time.deltaTime * smoothedMovementFactor);
// apply gravity before moving float verticalSmoothedFactor = isGrounded() ? 0 : inAirDampingVertical;
velocity.y += gravity * Time.deltaTime; if (velocity.y>0.0f) velocity.y = Mathf.Lerp(velocity.y, 0, Time.deltaTime * verticalSmoothedFactor);
/*==========Power Ups // Bullet management ===*/ /*==========Power Ups // Bullet management ===*/
if (Input.GetKeyDown(keyCodes[(int)Keys.Shoot])) if (Input.GetKeyDown(keyCodes[(int)Keys.Shoot]))
...@@ -162,7 +168,30 @@ public class PlayerControl : MonoBehaviour ...@@ -162,7 +168,30 @@ public class PlayerControl : MonoBehaviour
} }
} }
_controller.move(velocity * Time.deltaTime);
//finally set the velocity:
body2D.velocity = velocity;
//update move direction after the currentposition was updated:
moveDirection = (transform.position - lastPos).normalized;
}
private bool isGrounded()
{
Vector2 playerPos = new Vector2(transform.position.x, transform.position.y);
Vector2 groundPos = new Vector2(groundCheck.position.x, groundCheck.position.y);
bool result = Physics2D.Linecast(playerPos, groundPos, mask);
if (result)
{
Debug.DrawLine(playerPos, groundPos, Color.green, 0.5f, false);
}
else
{
Debug.DrawLine(playerPos, groundPos, Color.red, 0.5f, false);
}
return result;
} }
private void updateDashDirection() private void updateDashDirection()
......
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