Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GroupProject
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
TheRedTeam
GroupProject
Merge requests
!3
Feature physics
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Feature physics
feature_physics
into
develop
Overview
0
Commits
8
Pipelines
0
Changes
3
Merged
Clinton Galbraith
requested to merge
feature_physics
into
develop
7 years ago
Overview
0
Commits
8
Pipelines
0
Changes
3
Expand
0
0
Merge request reports
Viewing commit
36b3d845
Prev
Next
Show latest version
3 files
+
52
−
25
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
36b3d845
Added detailed comments to all scripts
· 36b3d845
clg544
authored
7 years ago
CMPT306_Fall2017/Assets/Scripts/PlayerBehavior.cs
0 → 100755
+
88
−
0
Options
using
System.Collections
;
using
System.Collections.Generic
;
using
UnityEngine
;
public
class
PlayerBehavior
:
MonoBehaviour
{
/* Movement to apply this frame */
private
Vector3
frameMovement
;
/* How much force is applied by player movement */
[
SerializeField
]
private
float
acceleration
;
/* How much the velocity is scaled down on brake */
[
SerializeField
]
private
float
brakeFraction
;
[
SerializeField
]
private
float
constantFraction
;
/* The maximum speed a player is allowed to go */
[
SerializeField
]
private
float
maxSpeed
;
/* Holds our plaayer and player body */
public
GameObject
player
;
private
Rigidbody2D
playerBody
;
/* Getters and Setters */
public
float
getMaxSpeed
()
{
return
maxSpeed
;
}
/**
* Collection of basic movement functions that add to a movement vector, which is
* reset every frame.
*/
public
void
MoveLeft
()
{
frameMovement
.
x
-=
acceleration
;
}
public
void
MoveRight
()
{
frameMovement
.
x
+=
acceleration
;
}
public
void
MoveUp
()
{
frameMovement
.
y
+=
acceleration
;
}
public
void
MoveDown
()
{
frameMovement
.
y
-=
acceleration
;
}
/**
* Scale the player's velocity down, if brakeFraction is < zero
*/
public
void
Brake
()
{
playerBody
.
velocity
*=
brakeFraction
;
}
// Use this for initialization
void
Start
()
{
playerBody
=
this
.
GetComponent
<
Rigidbody2D
>();
}
// Update is called once per frame
void
Update
()
{
/* Accelerate the player with a movement vector based on this frames input */
playerBody
.
AddForce
(
frameMovement
.
normalized
*
acceleration
);
/* Cap player speed with a velocity check */
if
(
playerBody
.
velocity
.
magnitude
>
maxSpeed
)
playerBody
.
velocity
=
(
playerBody
.
velocity
.
normalized
*
maxSpeed
);
/* Apply some simple friction */
playerBody
.
velocity
*=
constantFraction
;
/* Reset any persistant variables */
frameMovement
=
Vector3
.
zero
;
}
}
Loading