Commit d9334fea by Tim Reiter

player control working :)

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