Commit 4d6397f8 by Alisa Jung
parents 236844f5 76dcdfb3
fileFormatVersion: 2
guid: e937fe68fce80d44fb385a099114c29f
timeCreated: 1433439077
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine; using UnityEngine;
using System.Collections; using System.Collections;
public class PlayerControl : MonoBehaviour { public class PlayerControl : MonoBehaviour
{
#region members
public float gravity = -25f;
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;
[HideInInspector]
private float normalizedHorizontalSpeed = 0;
private CharacterController2D _controller;
private Vector3 _velocity;
#endregion
// Use this for initialization // Use this for initialization
void Start () { void Start () {
_controller = GetComponent<CharacterController2D>();
}
void Update()
{
// grab our current _velocity to use as a base for all calculations
_velocity = _controller.velocity;
if (_controller.isGrounded)
_velocity.y = 0;
if (Input.GetKey(KeyCode.RightArrow))
{
normalizedHorizontalSpeed = 1;
if (transform.localScale.x < 0f)
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
if (_controller.isGrounded)
{
//TODO animation run
}
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
normalizedHorizontalSpeed = -1;
if (transform.localScale.x > 0f)
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
if (_controller.isGrounded)
{
//TODO animation run
} }
}
else
{
normalizedHorizontalSpeed = 0;
if (_controller.isGrounded)
{
//TODO animation idle
}
}
// we can only jump whilst grounded
if (_controller.isGrounded && Input.GetKeyDown(KeyCode.UpArrow))
{
_velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity);
//TODO animation jump
}
// 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);
// Update is called once per frame // apply gravity before moving
void Update () { _velocity.y += gravity * Time.deltaTime;
_controller.move(_velocity * Time.deltaTime);
} }
} }
...@@ -3,6 +3,8 @@ using System.Collections; ...@@ -3,6 +3,8 @@ using System.Collections;
public class Spikes : MonoBehaviour { public class Spikes : MonoBehaviour {
float damage; //the amount of damage the spikes make.
// Use this for initialization // Use this for initialization
void Start () { void Start () {
...@@ -12,4 +14,14 @@ public class Spikes : MonoBehaviour { ...@@ -12,4 +14,14 @@ public class Spikes : MonoBehaviour {
void Update () { void Update () {
} }
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Player")
{
//TODO: do damage
}
}
} }
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment