Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dyingIsMainstream
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gamejam-gpn15
dyingIsMainstream
Commits
d9334fea
Commit
d9334fea
authored
Jun 04, 2015
by
Tim Reiter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
player control working :)
parent
76dcdfb3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
36 deletions
+52
-36
PlayerControl.cs
Assets/Scripts/PlayerControl.cs
+52
-36
game.unity
Assets/game.unity
+0
-0
No files found.
Assets/Scripts/PlayerControl.cs
View file @
d9334fea
using
UnityEngine
;
using
UnityEngine
;
using
System.Collections
;
using
System.Collections
;
public
class
PlayerControl
:
MonoBehaviour
public
class
PlayerControl
:
MonoBehaviour
{
{
// movement config
#
region
members
public
float
gravity
=
-
15f
;
public
float
gravity
=
-
25f
;
public
float
runSpeed
=
8f
;
public
float
runSpeed
=
8f
;
public
float
groundDamping
=
20f
;
// how fast do we change direction? higher means faster
public
float
groundDamping
=
20f
;
// how fast do we change direction? higher means faster
public
float
inAirDamping
=
5f
;
public
float
inAirDamping
=
5f
;
public
float
jumpHeight
=
3f
;
public
float
targetJumpHeight
=
2f
;
// public float jumpWaitTime = 2.0;
[
HideInInspector
]
[
HideInInspector
]
private
float
normalizedHorizontalSpeed
=
0
;
public
float
rawMovementDirection
=
1
;
private
CharacterController2D
_controller
;
//[HideInInspector]
private
Vector3
_velocity
;
public
float
normalizedHorizontalSpeed
=
0
;
#
endregion
// Use this for initialization
CharacterController2D
_controller
;
void
Start
()
{
public
RaycastHit2D
lastControllerColliderHit
;
[
HideInInspector
]
public
Vector3
velocity
;
void
Awake
()
{
_controller
=
GetComponent
<
CharacterController2D
>();
_controller
=
GetComponent
<
CharacterController2D
>();
_controller
.
onControllerCollidedEvent
+=
onControllerCollider
;
}
}
void
onControllerCollider
(
RaycastHit2D
hit
)
{
// bail out on plain old ground hits
if
(
hit
.
normal
.
y
==
1f
)
return
;
// logs any collider hits
//Debug.Log( "flags: " + _controller.collisionState + ", hit.normal: " + hit.normal );
}
void
Update
()
void
Update
()
{
{
// grab our current
_
velocity to use as a base for all calculations
// grab our current velocity to use as a base for all calculations
_
velocity
=
_controller
.
velocity
;
velocity
=
_controller
.
velocity
;
if
(
_controller
.
isGrounded
)
if
(
_controller
.
isGrounded
)
_
velocity
.
y
=
0
;
velocity
.
y
=
0
;
if
(
Input
.
GetKey
(
KeyCode
.
RightArrow
))
if
(
Input
.
GetKey
(
KeyCode
.
RightArrow
))
{
{
...
@@ -36,11 +57,7 @@ public class PlayerControl : MonoBehaviour
...
@@ -36,11 +57,7 @@ public class PlayerControl : MonoBehaviour
if
(
transform
.
localScale
.
x
<
0f
)
if
(
transform
.
localScale
.
x
<
0f
)
transform
.
localScale
=
new
Vector3
(-
transform
.
localScale
.
x
,
transform
.
localScale
.
y
,
transform
.
localScale
.
z
);
transform
.
localScale
=
new
Vector3
(-
transform
.
localScale
.
x
,
transform
.
localScale
.
y
,
transform
.
localScale
.
z
);
if
(
_controller
.
isGrounded
)
//if (_controller.isGrounded)
{
//TODO animation run
}
}
}
else
if
(
Input
.
GetKey
(
KeyCode
.
LeftArrow
))
else
if
(
Input
.
GetKey
(
KeyCode
.
LeftArrow
))
{
{
...
@@ -48,37 +65,35 @@ public class PlayerControl : MonoBehaviour
...
@@ -48,37 +65,35 @@ public class PlayerControl : MonoBehaviour
if
(
transform
.
localScale
.
x
>
0f
)
if
(
transform
.
localScale
.
x
>
0f
)
transform
.
localScale
=
new
Vector3
(-
transform
.
localScale
.
x
,
transform
.
localScale
.
y
,
transform
.
localScale
.
z
);
transform
.
localScale
=
new
Vector3
(-
transform
.
localScale
.
x
,
transform
.
localScale
.
y
,
transform
.
localScale
.
z
);
if
(
_controller
.
isGrounded
)
//if (_controller.isGrounded)
{
//TODO animation run
}
}
}
else
else
{
{
normalizedHorizontalSpeed
=
0
;
normalizedHorizontalSpeed
=
0
;
if
(
_controller
.
isGrounded
)
//if (_controller.isGrounded)
{
//TODO animation idle
}
}
}
// we can only jump whilst grounded
if
(
Input
.
GetKeyDown
(
KeyCode
.
UpArrow
))
if
(
_controller
.
isGrounded
&&
Input
.
GetKeyDown
(
KeyCode
.
UpArrow
))
{
{
_velocity
.
y
=
Mathf
.
Sqrt
(
2f
*
jumpHeight
*
-
gravity
);
//to avoid DOUBLE JUMP
//TODO animation jump
if
(
_controller
.
isGrounded
)
{
velocity
.
y
=
Mathf
.
Sqrt
(
2f
*
targetJumpHeight
*
-
gravity
);
}
}
}
// apply horizontal speed smoothing it
// apply horizontal speed smoothing it
var
smoothedMovementFactor
=
_controller
.
isGrounded
?
groundDamping
:
inAirDamping
;
// how fast do we change direction?
var
smoothedMovementFactor
=
_controller
.
isGrounded
?
groundDamping
:
inAirDamping
;
// how fast do we change direction?
_velocity
.
x
=
Mathf
.
Lerp
(
_velocity
.
x
,
normalizedHorizontalSpeed
*
runSpeed
,
Time
.
deltaTime
*
smoothedMovementFactor
);
velocity
.
x
=
Mathf
.
Lerp
(
velocity
.
x
,
normalizedHorizontalSpeed
*
rawMovementDirection
*
runSpeed
,
Time
.
deltaTime
*
smoothedMovementFactor
);
// apply gravity before moving
velocity
.
y
+=
gravity
*
Time
.
deltaTime
;
// apply gravity before moving
_controller
.
move
(
velocity
*
Time
.
deltaTime
);
_velocity
.
y
+=
gravity
*
Time
.
deltaTime
;
_controller
.
move
(
_velocity
*
Time
.
deltaTime
);
}
}
}
}
\ No newline at end of file
Assets/game.unity
View file @
d9334fea
No preview for this file type
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