using UnityEngine; using System.Collections; using Forces; public class Player : MonoBehaviour { public float weight = 20.0f; protected new Collider collider; protected Rigidbody rigidBody; protected Vector3 velocity; protected ForceController controller; public ForceProps[] forces; protected bool grounding; void Awake () { rigidBody = GetComponent(); controller = new ForceController(); collider = GetComponent(); } // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.3f) { float value = Mathf.Sign(Input.GetAxis("Horizontal")); controller.AddForce(forces[0], Vector3.right * value); } if (Input.GetButtonDown("A") && grounding) { controller.AddForce(forces[1], Vector3.up); } } void FixedUpdate () { Step(); } void OnLanded () { controller.DestroyForce(forces[1]); velocity.y = 0.0f; } void Step () { bool lastGrounded = grounding; velocity = rigidBody.velocity; velocity.y += -8.0f * weight * Time.fixedDeltaTime; Vector2 forceControllerVel = controller.Update(); velocity.x += forceControllerVel.x; if (controller.GetForce(forces[1]) != null) { Debug.Log(forceControllerVel.y); } velocity.y += forceControllerVel.y; grounding = IsGrounded(); if (lastGrounded != grounding) { if (grounding) OnLanded(); } rigidBody.velocity = velocity; } bool IsGrounded() { Vector3[] raycastPoints = { new Vector3(collider.bounds.max.x, collider.bounds.min.y + 0.1f, collider.bounds.center.z), new Vector3(collider.bounds.min.x, collider.bounds.min.y + 0.1f, collider.bounds.center.z), new Vector3(collider.bounds.center.x, collider.bounds.min.y + 0.1f, collider.bounds.center.z) }; for (int i = 0; i < raycastPoints.Length; i++) { //Debug.DrawRay(raycastPoints[i], Vector3.down * (0.1f + velocity.magnitude * Time.fixedDeltaTime)); if (Physics.Raycast(raycastPoints[i], Vector3.down, 0.1f + velocity.magnitude * Time.fixedDeltaTime)) { return true; } } return false; } }