Commit d9334fea by Tim Reiter

player control working :)

parent 76dcdfb3
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
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