Commit 14dbaa34 authored by Tanner Grehawick's avatar Tanner Grehawick

working CollisionStateRefactor

parent 8c74d0fa
No preview for this file type
......@@ -33,32 +33,37 @@ public class CollisionState : MonoBehaviour {
enemyHit = Physics2D.OverlapCircle(body.position + bottomPosition, collisionRadius, enemyLayer);
}
void OnDrawGizmosSelected() {
Gizmos.color = Color.red;
// void OnDrawGizmosSelected() {
// Gizmos.color = Color.red;
Vector2 pos = bottomPosition;
pos.x += transform.position.x;
pos.y += transform.position.y;
// Vector2 pos = bottomPosition;
// pos.x += transform.position.x;
// pos.y += transform.position.y;
Gizmos.DrawWireSphere(pos, collisionRadius);
// Gizmos.DrawWireSphere(pos, collisionRadius);
pos = rightPosition;
pos.x += transform.position.x;
pos.y += transform.position.y;
// pos = rightPosition;
// pos.x += transform.position.x;
// pos.y += transform.position.y;
Gizmos.DrawWireSphere(pos, collisionRadius);
// Gizmos.DrawWireSphere(pos, collisionRadius);
pos = leftPosition;
pos.x += transform.position.x;
pos.y += transform.position.y;
// pos = leftPosition;
// pos.x += transform.position.x;
// pos.y += transform.position.y;
Gizmos.DrawWireSphere(pos, collisionRadius);
}
// Gizmos.DrawWireSphere(pos, collisionRadius);
// }
float lastFixedTime;
void OnCollisionStay2D(Collision2D collision) {
onGround = false;
onWallLeft = false;
onWallRight = false;
if (Time.fixedTime != lastFixedTime) {
onGround = false;
onWallLeft = false;
onWallRight = false;
lastFixedTime = Time.fixedTime;
}
foreach (ContactPoint2D contact in collision.contacts) {
Color color = Color.white;
Vector2 normal = contact.normal;
......
......@@ -3,135 +3,135 @@ using System.Collections.Generic;
using UnityEngine;
public class Walk : AbstractBehavior {
public float speed;
private float airSpeed;
public float airAcceleration;
public float friction;
public float wallClingTime; //time you will 'stick' to the wall after pressing the opposite direction
public float clingSpeed = 0.1f;
public float realSpeed;
public bool wallSliding = false;
private int rightInt = 0;
private int leftInt = 0;
private float rightTime = 0;
private float leftTime = 0;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
Buttons left = inputButtons[0];
Buttons right = inputButtons[1];
rightInt = 0;
leftInt = 0;
if (inputState.GetButtonValue(right))
rightInt = 1;
else
rightInt = 0;
if (inputState.GetButtonValue(left))
leftInt = -1;
else
leftInt = 0;
wallSliding = false;
if (collisionState.onGround) //movement on ground
{
realSpeed = body.velocity.x;
if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left))
{
realSpeed = Mathf.Clamp(realSpeed + speed * (rightInt + leftInt), -speed, speed);
}
else
{
if (realSpeed != 0)
{
realSpeed += (-1) * Mathf.Sign(realSpeed) * friction;
if (Mathf.Abs(realSpeed) < friction)
realSpeed = 0;
}
}
body.velocity = new Vector2(realSpeed, body.velocity.y);
} else if (!collisionState.onWall || body.velocity.y > 0) //in air and not sliding
{
wallSliding = false;
airSpeed = body.velocity.x;
if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left))
{
if(Mathf.Abs(airSpeed) <= speed)
airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
else
{
if (airSpeed != 0)
{
airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
if (Mathf.Abs(airSpeed) < airAcceleration)
airSpeed = 0;
}
}
}
/*else //air deceleration while pressing nothing, I don't like this - may be removed
{
if (airSpeed != 0)
{
airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
if (Mathf.Abs(airSpeed) < airAcceleration)
airSpeed = 0;
}
}*/
body.velocity = new Vector2(airSpeed, body.velocity.y);
} else //handles wall sliding
{
wallSliding = true;
rightTime = inputState.GetButtonHoldTime(right);
leftTime = inputState.GetButtonHoldTime(left);
airSpeed = body.velocity.x;
//only sets a change if the button has been held for a set time
if (rightTime >= wallClingTime)
{
wallSliding = false;
airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
}
if (leftTime >= wallClingTime)
{
wallSliding = false;
airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
}
//same as if not on wall and not pressing the buttons
if (!inputState.GetButtonValue(right) && !inputState.GetButtonValue(left))
{
if (airSpeed != 0)
{
airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
if (Mathf.Abs(airSpeed) < airAcceleration)
airSpeed = 0;
}
}
body.velocity = new Vector2(airSpeed, body.velocity.y);
//slow down y velocity
if(body.velocity.y < clingSpeed)
{
body.velocity = new Vector2(body.velocity.x, -clingSpeed);
}
}
public float speed;
private float airSpeed;
public float airAcceleration;
public float friction;
public float wallClingTime; //time you will 'stick' to the wall after pressing the opposite direction
public float clingSpeed = 0.1f;
public float realSpeed;
public bool wallSliding = false;
private int rightInt = 0;
private int leftInt = 0;
private float rightTime = 0;
private float leftTime = 0;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
Buttons left = inputButtons[0];
Buttons right = inputButtons[1];
rightInt = 0;
leftInt = 0;
if (inputState.GetButtonValue(right))
rightInt = 1;
else
rightInt = 0;
if (inputState.GetButtonValue(left))
leftInt = -1;
else
leftInt = 0;
wallSliding = false;
if (collisionState.onGround) //movement on ground
{
realSpeed = body.velocity.x;
if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left))
{
realSpeed = Mathf.Clamp(realSpeed + speed * (rightInt + leftInt), -speed, speed);
}
else
{
if (realSpeed != 0)
{
realSpeed += (-1) * Mathf.Sign(realSpeed) * friction;
if (Mathf.Abs(realSpeed) < friction)
realSpeed = 0;
}
}
body.velocity = new Vector2(realSpeed, body.velocity.y);
} else if (!collisionState.onWall || body.velocity.y > 0) //in air and not sliding
{
wallSliding = false;
airSpeed = body.velocity.x;
if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left))
{
if(Mathf.Abs(airSpeed) <= speed)
airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
else
{
if (airSpeed != 0)
{
airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
if (Mathf.Abs(airSpeed) < airAcceleration)
airSpeed = 0;
}
}
}
/*else //air deceleration while pressing nothing, I don't like this - may be removed
{
if (airSpeed != 0)
{
airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
if (Mathf.Abs(airSpeed) < airAcceleration)
airSpeed = 0;
}
}*/
body.velocity = new Vector2(airSpeed, body.velocity.y);
} else //handles wall sliding
{
wallSliding = true;
rightTime = inputState.GetButtonHoldTime(right);
leftTime = inputState.GetButtonHoldTime(left);
airSpeed = body.velocity.x;
//only sets a change if the button has been held for a set time
if (rightTime >= wallClingTime)
{
wallSliding = false;
airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
}
if (leftTime >= wallClingTime)
{
wallSliding = false;
airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
}
//same as if not on wall and not pressing the buttons
if (!inputState.GetButtonValue(right) && !inputState.GetButtonValue(left))
{
if (airSpeed != 0)
{
airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
if (Mathf.Abs(airSpeed) < airAcceleration)
airSpeed = 0;
}
}
body.velocity = new Vector2(airSpeed, body.velocity.y);
//slow down y velocity
if(body.velocity.y < clingSpeed)
{
body.velocity = new Vector2(body.velocity.x, -clingSpeed);
}
}
//change localScale depending on direction
if (inputState.GetButtonValue (right) && inputState.GetButtonValue (left))
......@@ -144,5 +144,5 @@ public class Walk : AbstractBehavior {
{
inputState.direction = Directions.Left;
}
}
}
}
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