Skip to content
Snippets Groups Projects
Commit b6493cee authored by Robin Halseth Sandvik's avatar Robin Halseth Sandvik
Browse files

Generalized TryMove() to Character.

parent 6b060ea8
No related branches found
No related tags found
1 merge request!15Fixing knockback & movement.
...@@ -98,6 +98,17 @@ namespace BigSock { ...@@ -98,6 +98,17 @@ namespace BigSock {
} }
/*
Try to move in a given direction.
*/
protected virtual bool TryMove(Vector2 direction) {
if(direction != Vector2.zero) {
rb.AddForce(direction * (float) MovementSpeed * Time.fixedDeltaTime, ForceMode2D.Impulse);
return true;
}
return false;
}
/* /*
Adds damage to the player if they don't have IFrames. Adds damage to the player if they don't have IFrames.
*/ */
......
...@@ -104,15 +104,8 @@ namespace BigSock { ...@@ -104,15 +104,8 @@ namespace BigSock {
} }
} }
private bool TryMove(Vector2 direction) {
if(direction != Vector2.zero) {
rb.AddForce(direction * (float) MovementSpeed * Time.fixedDeltaTime, ForceMode2D.Impulse);
return true;
}
return false;
}
private bool TryMove_OLD(Vector2 direction) { private bool TryMove_OLD(Vector2 direction) {
if(direction != Vector2.zero) { if(direction != Vector2.zero) {
......
...@@ -118,7 +118,8 @@ namespace BigSock { ...@@ -118,7 +118,8 @@ namespace BigSock {
var temp = (pos - rb.transform.position); var temp = (pos - rb.transform.position);
temp.z = 0; temp.z = 0;
var direction = temp.normalized; var direction = temp.normalized;
rb.AddForce((float) LeapForce * direction, ForceMode2D.Impulse); //rb.AddForce((float) LeapForce * direction, ForceMode2D.Impulse);
TryMove(direction);
// Update the state. // Update the state.
State = SlimeState.Leaping; State = SlimeState.Leaping;
...@@ -139,36 +140,18 @@ namespace BigSock { ...@@ -139,36 +140,18 @@ namespace BigSock {
} }
private bool TryMove(Vector2 direction) {
/*
Try to move in a given direction.
*/
protected override bool TryMove(Vector2 direction) {
if(direction != Vector2.zero) { if(direction != Vector2.zero) {
rb.AddForce((float) LeapForce * direction, ForceMode2D.Impulse);
// Check for potential collisions return true;
int count = rb.Cast(
direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
castCollisions, // List of collisions to store the found collisions into after the Cast is finished
(float) MovementSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
//Debug.Log($"cast {string.Join(", ", castCollisions.Select(x => $"{x.collider.isTrigger}"))}");
//Debug.Log($"rb.position : {rb.position}");
//Debug.Log($"target.position : {target.position}");
//Debug.Log($"direction : {direction}");
if(count == 0){
rb.MovePosition(rb.position + direction * (float) MovementSpeed * Time.fixedDeltaTime);
//print($"rb.position {rb.position}");
//print($"direction {direction}");
RotateAnimation(direction);
return true;
} else {
return false;
}
} else {
// Can't move if there's no direction to move in
return false;
} }
return false;
} }
} }
......
...@@ -78,13 +78,7 @@ namespace BigSock { ...@@ -78,13 +78,7 @@ namespace BigSock {
} }
} }
private bool TryMove(Vector2 direction) {
if(direction != Vector2.zero) {
rb.AddForce(direction * (float) MovementSpeed * Time.fixedDeltaTime, ForceMode2D.Impulse);
return true;
}
return false;
}
private bool TryMove_OLD(Vector2 direction) { private bool TryMove_OLD(Vector2 direction) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment