Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Mr BigSock
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gard Aleksander Furre
Mr BigSock
Merge requests
!12
Made attacks damage the enemy.
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Made attacks damage the enemy.
master
into
main
Overview
0
Commits
5
Pipelines
0
Changes
20
Merged
Robin Halseth Sandvik
requested to merge
master
into
main
2 years ago
Overview
0
Commits
5
Pipelines
0
Changes
20
Expand
OnColide for projectiles.
Layers to prevent collision between player and bullets.
Commented out some unused print statements.
Added hp handling to EnemyController.
Added slime enemy
0
0
Merge request reports
Compare
main
version 2
2b52f253
2 years ago
version 1
e7d05c47
2 years ago
main (base)
and
latest version
latest version
0e92f38f
5 commits,
2 years ago
version 2
2b52f253
4 commits,
2 years ago
version 1
e7d05c47
3 commits,
2 years ago
20 files
+
934
−
136
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
20
Search (e.g. *.vue) (Ctrl+P)
MrBigsock/Assets/Code/Slime/SlimeController.cs
0 → 100644
+
235
−
0
Options
using
System.Collections
;
using
System.Collections.Generic
;
using
UnityEngine
;
using
System.Linq
;
using
System
;
namespace
BigSock
{
public
partial
class
SlimeController
:
Character
{
public
float
collisionOffset
=
0.05f
;
public
ContactFilter2D
movementFilter
;
List
<
RaycastHit2D
>
castCollisions
=
new
List
<
RaycastHit2D
>();
private
Transform
target
;
Animator
m_Animator
;
private
float
distance
;
private
float
canAttack
;
private
EmptyCollider
followCollider
;
private
EmptyCollider
attackCollider
;
private
bool
isInMelee
=
false
;
Rigidbody2D
rb
;
// private Collider2D_Proxy secondCollider;
/*
The state the slime is in.
*/
public
SlimeState
State
{
get
;
protected
set
;
}
=
SlimeState
.
Idle
;
/*
The location of the target when charging started.
*/
public
Vector3
TargetLocation
{
get
;
protected
set
;
}
/*
The next time the slime can change its state.
*/
public
DateTime
NextTimeStateCanChange
{
get
;
private
set
;
}
=
DateTime
.
Now
;
/*
Minimum time the slime should idle before it can charge.
*/
protected
static
readonly
TimeSpan
IDLE_WAIT_TIME
=
new
TimeSpan
(
0
,
0
,
0
,
1
,
0
);
/*
Minimum time the slime should charge before it can leap.
*/
protected
static
readonly
TimeSpan
CHARGE_WAIT_TIME
=
new
TimeSpan
(
0
,
0
,
0
,
2
,
0
);
/*
Maximum time the slime should leap before it can idle.
*/
protected
static
readonly
TimeSpan
LEAP_WAIT_TIME
=
new
TimeSpan
(
0
,
0
,
0
,
4
,
0
);
/*
The force the slime leaps at.
*/
public
double
LeapForce
=>
MovementSpeed
*
4
;
void
Start
()
{
rb
=
GetComponent
<
Rigidbody2D
>();
m_Animator
=
gameObject
.
GetComponent
<
Animator
>();
followCollider
=
transform
.
Find
(
"followCollider"
).
GetComponent
<
EmptyCollider
>();
followCollider
.
OnColliderEnter2D_Action
+=
Move_OnColliderEnter2D
;
followCollider
.
OnColliderStay2D_Action
+=
Move_OnColliderStay2D
;
followCollider
.
OnColliderExit2D_Action
+=
Move_OnColliderExit2D
;
attackCollider
=
transform
.
Find
(
"MeleeCollider"
).
GetComponent
<
EmptyCollider
>();
attackCollider
.
OnColliderEnter2D_Action
+=
Attack_OnColliderEnter2D
;
attackCollider
.
OnColliderStay2D_Action
+=
Attack_OnColliderStay2D
;
attackCollider
.
OnColliderExit2D_Action
+=
Attack_OnColliderExit2D
;
}
private
void
RotateAnimation
(
Vector2
direction
)
{
if
(
direction
.
x
>
0.01f
){
gameObject
.
GetComponent
<
SpriteRenderer
>().
flipX
=
false
;
}
else
if
(
direction
.
x
<
-
0.01f
){
gameObject
.
GetComponent
<
SpriteRenderer
>().
flipX
=
true
;
}
}
private
void
Update
()
{
if
(
State
==
SlimeState
.
Idle
)
{
// If it has a target and has idled long enough.
if
(
target
!=
null
&&
DateTime
.
Now
>=
NextTimeStateCanChange
)
{
// Store target location.
TargetLocation
=
target
.
position
;
// Update the state.
State
=
SlimeState
.
Charging
;
NextTimeStateCanChange
=
DateTime
.
Now
+
CHARGE_WAIT_TIME
;
m_Animator
.
SetTrigger
(
"walk"
);
}
}
else
if
(
State
==
SlimeState
.
Charging
)
{
// If it has charged long enough.
if
(
DateTime
.
Now
>=
NextTimeStateCanChange
)
{
// Set momentum
var
pos
=
TargetLocation
;
//target.position;
var
temp
=
(
pos
-
rb
.
transform
.
position
);
temp
.
z
=
0
;
var
direction
=
temp
.
normalized
;
rb
.
AddForce
((
float
)
LeapForce
*
direction
,
ForceMode2D
.
Impulse
);
// Update the state.
State
=
SlimeState
.
Leaping
;
NextTimeStateCanChange
=
DateTime
.
Now
+
LEAP_WAIT_TIME
;
}
}
else
if
(
State
==
SlimeState
.
Leaping
)
{
// If it has charged long enough.
if
(
DateTime
.
Now
>=
NextTimeStateCanChange
||
rb
.
velocity
==
Vector2
.
zero
)
{
// Update the state.
State
=
SlimeState
.
Idle
;
NextTimeStateCanChange
=
DateTime
.
Now
+
IDLE_WAIT_TIME
;
m_Animator
.
SetTrigger
(
"idle"
);
}
}
}
private
bool
TryMove
(
Vector2
direction
)
{
if
(
direction
!=
Vector2
.
zero
)
{
// Check for potential collisions
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
;
}
}
}
/*
Attack
*/
public
partial
class
SlimeController
{
private
void
Attack_OnColliderEnter2D
(
Collider2D
other
){
if
(
other
.
gameObject
.
tag
==
"Player"
)
isInMelee
=
true
;
}
private
void
Attack_OnColliderStay2D
(
Collider2D
other
){
var
player
=
other
.
gameObject
.
GetComponent
<
PlayerController
>();
if
(
player
!=
null
){
if
(
player
.
TakeDamage
(
Damage
))
{
//knockback ?
//animer nå ?
}
}
}
private
void
Attack_OnColliderExit2D
(
Collider2D
other
){
if
(
other
.
gameObject
.
tag
==
"Player"
)
isInMelee
=
false
;
}
}
/*
Movement
*/
public
partial
class
SlimeController
{
private
void
Move_OnColliderEnter2D
(
Collider2D
other
)
{
if
(
other
.
gameObject
.
tag
==
"Player"
)
{
//m_Animator.SetTrigger("walk");
target
=
other
.
transform
;
}
}
private
void
Move_OnColliderStay2D
(
Collider2D
other
)
{
if
(
other
.
gameObject
.
tag
==
"Player"
)
{
}
}
private
void
Move_OnColliderExit2D
(
Collider2D
other
)
{
if
(
other
.
gameObject
.
tag
==
"Player"
)
{
//m_Animator.SetTrigger("idle");
target
=
null
;
}
}
}
/*
The different states the slime can be in.
*/
public
enum
SlimeState
{
Idle
,
Charging
,
Leaping
}
}
Loading