Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
Birb
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gadig
Birb
Commits
14dbaa34
Commit
14dbaa34
authored
Mar 07, 2017
by
Tanner Grehawick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
working CollisionStateRefactor
parent
8c74d0fa
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
153 additions
and
148 deletions
+153
-148
Assets/Scenes/CharacterTest.unity
Assets/Scenes/CharacterTest.unity
+0
-0
Assets/Scripts/Player/CollisionState.cs
Assets/Scripts/Player/CollisionState.cs
+23
-18
Assets/Scripts/Player/Walk.cs
Assets/Scripts/Player/Walk.cs
+130
-130
No files found.
Assets/Scenes/CharacterTest.unity
100644 → 100755
View file @
14dbaa34
No preview for this file type
Assets/Scripts/Player/CollisionState.cs
View file @
14dbaa34
...
...
@@ -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
;
...
...
Assets/Scripts/Player/Walk.cs
View file @
14dbaa34
...
...
@@ -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
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment