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
;
public
class
PlayerControl
:
MonoBehaviour
{
#
region
members
public
float
gravity
=
-
25f
;
// movement config
public
float
gravity
=
-
15f
;
public
float
runSpeed
=
8f
;
public
float
groundDamping
=
20f
;
// how fast do we change direction? higher means faster
public
float
inAirDamping
=
5f
;
public
float
jumpHeight
=
3f
;
public
float
targetJumpHeight
=
2f
;
// public float jumpWaitTime = 2.0;
[
HideInInspector
]
private
float
normalizedHorizontalSpeed
=
0
;
private
CharacterController2D
_controller
;
private
Vector3
_velocity
;
#
endregion
public
float
rawMovementDirection
=
1
;
//[HideInInspector]
public
float
normalizedHorizontalSpeed
=
0
;
// Use this for initialization
void
Start
()
{
CharacterController2D
_controller
;
public
RaycastHit2D
lastControllerColliderHit
;
[
HideInInspector
]
public
Vector3
velocity
;
void
Awake
()
{
_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
()
{
// grab our current
_
velocity to use as a base for all calculations
_
velocity
=
_controller
.
velocity
;
// grab our current velocity to use as a base for all calculations
velocity
=
_controller
.
velocity
;
if
(
_controller
.
isGrounded
)
_
velocity
.
y
=
0
;
velocity
.
y
=
0
;
if
(
Input
.
GetKey
(
KeyCode
.
RightArrow
))
{
...
...
@@ -36,11 +57,7 @@ public class PlayerControl : MonoBehaviour
if
(
transform
.
localScale
.
x
<
0f
)
transform
.
localScale
=
new
Vector3
(-
transform
.
localScale
.
x
,
transform
.
localScale
.
y
,
transform
.
localScale
.
z
);
if
(
_controller
.
isGrounded
)
{
//TODO animation run
}
//if (_controller.isGrounded)
}
else
if
(
Input
.
GetKey
(
KeyCode
.
LeftArrow
))
{
...
...
@@ -48,37 +65,35 @@ public class PlayerControl : MonoBehaviour
if
(
transform
.
localScale
.
x
>
0f
)
transform
.
localScale
=
new
Vector3
(-
transform
.
localScale
.
x
,
transform
.
localScale
.
y
,
transform
.
localScale
.
z
);
if
(
_controller
.
isGrounded
)
{
//TODO animation run
}
//if (_controller.isGrounded)
}
else
{
normalizedHorizontalSpeed
=
0
;
if
(
_controller
.
isGrounded
)
{
//TODO animation idle
}
//if (_controller.isGrounded)
}
// we can only jump whilst grounded
if
(
_controller
.
isGrounded
&&
Input
.
GetKeyDown
(
KeyCode
.
UpArrow
))
if
(
Input
.
GetKeyDown
(
KeyCode
.
UpArrow
))
{
_velocity
.
y
=
Mathf
.
Sqrt
(
2f
*
jumpHeight
*
-
gravity
);
//TODO animation jump
//to avoid DOUBLE JUMP
if
(
_controller
.
isGrounded
)
{
velocity
.
y
=
Mathf
.
Sqrt
(
2f
*
targetJumpHeight
*
-
gravity
);
}
}
// apply horizontal speed smoothing it
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
_velocity
.
y
+=
gravity
*
Time
.
deltaTime
;
_controller
.
move
(
velocity
*
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