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
db9e221a
Commit
db9e221a
authored
Mar 06, 2017
by
Tanner Grehawick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
directional dash
parent
f39acef8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
87 deletions
+93
-87
Assets/Scenes/CharacterTest.unity
Assets/Scenes/CharacterTest.unity
+0
-0
Assets/Scripts/Birb/Dash.cs
Assets/Scripts/Birb/Dash.cs
+93
-87
No files found.
Assets/Scenes/CharacterTest.unity
View file @
db9e221a
No preview for this file type
Assets/Scripts/Birb/Dash.cs
View file @
db9e221a
using
System.Collections
;
using
System.Collections
;
using
System.Collections.Generic
;
using
UnityEngine
;
...
...
@@ -7,101 +7,107 @@ using UnityEngine;
public
class
Dash
:
AbstractBehavior
{
public
float
coolDown
=
1f
;
public
float
dashforce
=
5
;
public
float
holdTime
=
0.4f
;
public
float
minVelocity
=
0.5f
;
private
bool
_isDashing
=
false
;
private
Transform
dashMeter
;
private
float
lastDash
;
private
CircleCollider2D
collider
;
// Call to check if currently dashing
public
bool
isDashing
{
get
{
return
_isDashing
;
}
private
set
{
_isDashing
=
value
;
}
}
void
Start
()
{
dashMeter
=
GameObject
.
FindGameObjectWithTag
(
"DashMeter"
).
transform
;
collider
=
GetComponent
<
CircleCollider2D
>();
}
public
float
coolDown
=
1f
;
public
float
dashforce
=
5
;
public
float
holdTime
=
0.4f
;
public
float
minVelocity
=
0.5f
;
private
bool
_isDashing
=
false
;
private
Transform
dashMeter
;
private
float
lastDash
;
private
CircleCollider2D
collider
;
// Call to check if currently dashing
public
bool
isDashing
{
get
{
return
_isDashing
;
}
private
set
{
_isDashing
=
value
;
}
}
void
Update
()
{
DashCheck
();
void
Start
()
{
dashMeter
=
GameObject
.
FindGameObjectWithTag
(
"DashMeter"
).
transform
;
collider
=
GetComponent
<
CircleCollider2D
>();
}
// Should be called every frame
public
void
DashCheck
(
)
{
// Button pressed, not in cooldown, and not on wall
if
(
inputState
.
GetButtonValue
(
inputButtons
[
0
])
&&
Time
.
time
>
lastDash
&&
!
collisionState
.
onWall
)
{
StartCoroutine
(
DoDash
()
);
}
void
Update
()
{
if
(
inputState
.
GetButtonValue
(
inputButtons
[
0
])
)
{
Activate
();
}
UpdateUI
(
);
}
UpdateUI
();
}
// Should be called every frame
public
void
Activate
(
Directions
direction
)
{
// not in cooldown and not on wall
if
(
Time
.
time
>
lastDash
&&
!
collisionState
.
onWall
)
{
StartCoroutine
(
DashRoutine
(
direction
));
}
}
public
void
Activate
()
{
Activate
(
inputState
.
direction
);
}
void
UpdateUI
()
{
void
UpdateUI
()
{
float
progress
=
1
-
((
lastDash
-
Time
.
time
)/
coolDown
);
dashMeter
.
localScale
=
new
Vector2
(
Mathf
.
Clamp01
(
progress
),
dashMeter
.
localScale
.
y
);
}
// Dash
IEnumerator
DoDash
()
{
// Cooldown
lastDash
=
Time
.
time
+
coolDown
;
// Is dashing
isDashing
=
true
;
// Set initial velocity
body
.
velocity
=
transform
.
right
*
dashforce
;
// Disable gravity
float
initialGrav
=
body
.
gravityScale
;
body
.
gravityScale
=
0
;
// results for circle cast
RaycastHit2D
[]
results
=
new
RaycastHit2D
[
8
];
// Decelerate
for
(
float
i
=
0
;
i
<
holdTime
;
i
+=
Time
.
deltaTime
)
{
// Calculate velocity
Vector2
targetVel
=
(
transform
.
right
*
dashforce
)
*
(
1
-
(
i
/
holdTime
));
if
(
body
.
velocity
.
magnitude
<
minVelocity
||
collisionState
.
onWall
)
{
// Velocity is low enough or collision with wall
break
;
}
// Apply velocity
body
.
velocity
=
targetVel
;
float
checkDistance
=
body
.
velocity
.
magnitude
*
Time
.
deltaTime
;
// check for breakable in front
int
resultCount
=
Physics2D
.
CircleCastNonAlloc
(
body
.
position
,
collider
.
radius
,
body
.
velocity
,
results
,
checkDistance
);
for
(
int
r
=
0
;
r
<
resultCount
;
r
++)
{
RaycastHit2D
result
=
results
[
r
];
Breakable
breakable
=
result
.
collider
.
GetComponent
<
Breakable
>();
if
(
breakable
!=
null
&&
breakable
.
breakOnPlayerDash
)
{
breakable
.
Break
();
}
dashMeter
.
localScale
=
new
Vector2
(
Mathf
.
Clamp01
(
progress
),
dashMeter
.
localScale
.
y
);
}
// Dash
IEnumerator
DashRoutine
(
Directions
direction
)
{
// dash direction vector
Vector2
dir
=
((
int
)
direction
)
*
Vector2
.
right
;
// Cooldown
lastDash
=
Time
.
time
+
coolDown
;
// Is dashing
isDashing
=
true
;
// Set initial velocity
body
.
velocity
=
dir
*
dashforce
;
// Disable gravity
float
initialGrav
=
body
.
gravityScale
;
body
.
gravityScale
=
0
;
// results for circle cast
RaycastHit2D
[]
results
=
new
RaycastHit2D
[
8
];
// Decelerate
for
(
float
i
=
0
;
i
<
holdTime
;
i
+=
Time
.
deltaTime
)
{
// Calculate velocity
Vector2
targetVel
=
(
dir
*
dashforce
)
*
(
1
-
(
i
/
holdTime
));
if
(
body
.
velocity
.
magnitude
<
minVelocity
||
collisionState
.
onWall
)
{
// Velocity is low enough or collision with wall
break
;
}
// Apply velocity
body
.
velocity
=
targetVel
;
float
checkDistance
=
body
.
velocity
.
magnitude
*
Time
.
deltaTime
;
// check for breakable in front
int
resultCount
=
Physics2D
.
CircleCastNonAlloc
(
body
.
position
,
collider
.
radius
,
dir
,
results
,
checkDistance
);
for
(
int
r
=
0
;
r
<
resultCount
;
r
++)
{
RaycastHit2D
result
=
results
[
r
];
Breakable
breakable
=
result
.
collider
.
GetComponent
<
Breakable
>();
if
(
breakable
!=
null
&&
breakable
.
breakOnPlayerDash
)
{
breakable
.
Break
();
}
}
// Wait
yield
return
new
WaitForEndOfFrame
();
}
// Wait
yield
return
new
WaitForEndOfFrame
();
}
// Restore gravity
body
.
gravityScale
=
initialGrav
;
// Is not dashing
isDashing
=
false
;
}
// Restore gravity
body
.
gravityScale
=
initialGrav
;
// Is not dashing
isDashing
=
false
;
}
public
void
DashIgnoringCooldown
(
)
{
StartCoroutine
(
D
oDash
(
));
public
void
ActivateIgnoringCooldown
(
Directions
direction
)
{
StartCoroutine
(
D
ashRoutine
(
direction
));
}
public
void
ActivateIgnoringCooldown
()
{
ActivateIgnoringCooldown
(
inputState
.
direction
);
}
}
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