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
6a52a6fb
Commit
6a52a6fb
authored
Mar 02, 2017
by
Fl00rMaster
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ranged Enemy
parent
7b321b5c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
123 additions
and
0 deletions
+123
-0
Assets/Prefabs/enemyProjectile.prefab
Assets/Prefabs/enemyProjectile.prefab
+0
-0
Assets/Prefabs/enemyRanged.prefab
Assets/Prefabs/enemyRanged.prefab
+0
-0
Assets/Scripts/Enemy/enemyProjectile.cs
Assets/Scripts/Enemy/enemyProjectile.cs
+45
-0
Assets/Scripts/Enemy/enemyRanged.cs
Assets/Scripts/Enemy/enemyRanged.cs
+78
-0
No files found.
Assets/Prefabs/enemyProjectile.prefab
0 → 100644
View file @
6a52a6fb
File added
Assets/Prefabs/enemyRanged.prefab
0 → 100644
View file @
6a52a6fb
File added
Assets/Scripts/Enemy/enemyProjectile.cs
0 → 100644
View file @
6a52a6fb
using
System.Collections
;
using
System.Collections.Generic
;
using
UnityEngine
;
public
class
enemyProjectile
:
MonoBehaviour
{
float
travelDistance
;
//how far it can go
Vector2
start
;
//the starting point of the projectile
public
void
OnTriggerEnter2D
(
Collider2D
other
)
{
Debug
.
Log
(
"collided"
);
if
(!
other
.
gameObject
.
CompareTag
(
"Enemy"
))
{
if
(
other
.
gameObject
.
CompareTag
(
"Player"
))
{
//damage player
}
Destroy
(
this
.
gameObject
);
}
}
void
Start
()
{
start
=
transform
.
position
;
}
void
Update
()
{
if
(
new
Vector2
(
transform
.
position
.
x
-
start
.
x
,
transform
.
position
.
y
-
start
.
y
).
magnitude
>
travelDistance
)
//if the projectile has travelled less than the max distance
{
Destroy
(
this
.
gameObject
);
}
}
public
void
ProjectileExpiration
(
Vector2
velocity
,
float
travelDistance
)
{
GetComponent
<
Rigidbody2D
>().
velocity
=
velocity
;
this
.
travelDistance
=
travelDistance
;
}
}
Assets/Scripts/Enemy/enemyRanged.cs
0 → 100644
View file @
6a52a6fb
using
System.Collections
;
using
System.Collections.Generic
;
using
UnityEngine
;
public
class
enemyRanged
:
BaseEnemy
{
public
Collider2D
fieldOfView
;
//the 2D collider trigger on the enemy's child object checking if the player is in range to fire
public
GameObject
projectile
;
//the prefab of enemy projectiles
public
float
maxDistance
;
//the distance projectiles can travel;
public
float
projectileSpeed
;
//how fast the projectiles travel
public
float
fireCooldown
;
//the time between enemy attacks in units of seconds
float
fireTimer
;
public
bool
isFacingRight
;
//modifies the velocity of the fired projectile according to direction
public
float
angleOfSpread
;
//the greatest possible angle from the center projectiles can randomly spread; ******entered in radians*******
void
Start
()
{
fireTimer
=
0
;
if
(!
isFacingRight
)
fieldOfView
.
gameObject
.
transform
.
localPosition
=
new
Vector2
(-
fieldOfView
.
gameObject
.
transform
.
localPosition
.
x
,
fieldOfView
.
gameObject
.
transform
.
localPosition
.
y
);
}
void
Update
()
{
if
(
fieldOfView
.
IsTouching
(
GameObject
.
Find
(
"Player"
).
GetComponent
<
Collider2D
>()))
//if the player is in view
{
if
(
fireTimer
<=
0
)
{
Attack
();
fireTimer
=
fireCooldown
;
}
}
if
(
fireTimer
>
0
)
{
fireTimer
-=
Time
.
deltaTime
;
}
}
void
PositionFOV
()
//run to properly position the enemy's fov
{
}
void
Attack
()
{
float
generatedAngle
=
Random
.
Range
(-
angleOfSpread
,
angleOfSpread
);
Vector2
directionModifier
=
new
Vector2
(
Mathf
.
Cos
(
generatedAngle
),
Mathf
.
Sin
(
generatedAngle
));
//unit vector for projectile spread
GameObject
projectileInstance
=
Instantiate
(
projectile
,
gameObject
.
transform
);
projectileInstance
.
transform
.
position
=
transform
.
position
;
if
(!
isFacingRight
)
{
projectileInstance
.
GetComponent
<
enemyProjectile
>().
ProjectileExpiration
(
new
Vector2
(-
projectileSpeed
*
directionModifier
.
x
,
projectileSpeed
*
directionModifier
.
y
),
maxDistance
);
}
else
{
projectileInstance
.
GetComponent
<
enemyProjectile
>().
ProjectileExpiration
(
new
Vector2
(
projectileSpeed
*
directionModifier
.
x
,
projectileSpeed
*
directionModifier
.
y
),
maxDistance
);
}
// StartCoroutine(ProjectileExpiration(projectileInstance, projectileInstance.transform.position));
//coroutine uses the instance of the projectile and its starting position before moving
}
/*IEnumerator ProjectileExpiration(GameObject projectileInstance, Vector3 start) //coroutine which runs to check the projectile travels only its max distance
{
while(new Vector2(projectileInstance.transform.position.x - start.x,
projectileInstance.transform.position.y - start.y).magnitude < maxDistance) //while the projectile has travelled less than the max distance
{
yield return null;
}
Destroy(projectileInstance);
}*/
}
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