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
{
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
public float pushForce = 20f;
public float gravity = -15f;
public float runSpeed = 8f;
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 lastDashStart = float.NegativeInfinity;
[HideInInspector]
......@@ -33,9 +42,6 @@ public class PlayerControl : MonoBehaviour
//[HideInInspector]
public float normalizedHorizontalSpeed = 0;
CharacterController2D _controller;
public RaycastHit2D lastControllerColliderHit;
[HideInInspector]
public Vector3 velocity;
......@@ -44,8 +50,7 @@ public class PlayerControl : MonoBehaviour
void Start()
{
_controller = GetComponent<CharacterController2D>();
_controller.onControllerCollidedEvent += onControllerCollider;
body2D = GetComponent<Rigidbody2D>();
switch (playerNumber)
{
......@@ -81,18 +86,19 @@ public class PlayerControl : MonoBehaviour
if (body)
{
//TODO abfrage " is dashing"
body.AddForce(_controller.getDirectionOfMovement() * pushForce);
body.AddForce(moveDirection * pushForce);
lastDashStart = Time.time - dashCompletionTime;
currentDashCoolDown = dashCollisionCoolDown;
}
}
void Update()
void FixedUpdate()
{
// grab our current velocity to use as a base for all calculations
velocity = _controller.velocity;
//Update last position:
lastPos = transform.position;
if (_controller.isGrounded)
if (isGrounded())
velocity.y = 0;
updateDashDirection();
......@@ -110,45 +116,45 @@ public class PlayerControl : MonoBehaviour
}
else
{
velocity = body2D.velocity;
if (Input.GetKey(keyCodes[(int)Keys.Right]))
{
normalizedHorizontalSpeed = 1;
if (transform.localScale.x < 0f)
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
//normalizedHorizontalSpeed = 1;
//if (transform.localScale.x < 0f)
// transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
//if (_controller.isGrounded)
}
else if (Input.GetKey(keyCodes[(int)Keys.Left]))
{
normalizedHorizontalSpeed = -1;
if (transform.localScale.x > 0f)
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
//go right:
velocity = new Vector2(runSpeed,velocity.y);
//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 (Input.GetKey (keyCodes[(int)Keys.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
var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction?
velocity.x = Mathf.Lerp(velocity.x, normalizedHorizontalSpeed * rawMovementDirection * runSpeed, Time.deltaTime * smoothedMovementFactor);
var smoothedMovementFactor = isGrounded() ? groundDamping : inAirDamping; // how fast do we change direction?
velocity.x = Mathf.Lerp(velocity.x,0, Time.deltaTime * smoothedMovementFactor);
// apply gravity before moving
velocity.y += gravity * Time.deltaTime;
float verticalSmoothedFactor = isGrounded() ? 0 : inAirDampingVertical;
if (velocity.y>0.0f) velocity.y = Mathf.Lerp(velocity.y, 0, Time.deltaTime * verticalSmoothedFactor);
/*==========Power Ups // Bullet management ===*/
if (Input.GetKeyDown(keyCodes[(int)Keys.Shoot]))
......@@ -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()
......
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