Newer
Older
fall / FallUnity / Assets / Player / PlayerController.cs
using System;
using System.Collections;
using System.Collections.Generic;
using PlayerInput;
using UnityEngine;

[SelectionBase]
public class PlayerController : MonoBehaviour {
    public float Speed = 5;
    public float Gravity = 7;

    private CharacterController charC;
    private AnimationHandler anim;
	// Use this for initialization
	void Start () {
        charC = GetComponent<CharacterController>();
        anim = GetComponent<AnimationHandler>();
	}

    private float velY = 0;
	
	// Update is called once per frame
	void Update () {
        Vector3 dir = new Vector3();
        if (input != null)
        {
            if (charC.isGrounded)
            {
                dir.x = input.Axis(InputSource<PlayerController>.InputAxis.MoveX);
                dir.z = input.Axis(InputSource<PlayerController>.InputAxis.MoveY);

                if (dir.magnitude > 0.3f)
                {
                    dir.Normalize();
                    dir *= Speed;

                    float angle = Mathf.Atan2(dir.z, dir.x);

                    if (angle < 0) angle += Mathf.PI * 2;

                    if (angle < Mathf.PI / 2.0f) anim.SetDir(AnimationHandler.Direction.UpRight);
                    if (angle < Mathf.PI && angle >= Mathf.PI / 2.0f) anim.SetDir(AnimationHandler.Direction.UpLeft);
                    if (angle < Mathf.PI * 3.0f / 2.0f && angle >= Mathf.PI) anim.SetDir(AnimationHandler.Direction.DownLeft);
                    if (angle >= Mathf.PI * 3.0f / 2.0f) anim.SetDir(AnimationHandler.Direction.DownRight);

                    //Debug.Log("ANGLE: " + angle);
                }
                else
                {
                    dir.x = 0;
                    dir.z = 0;
                }
            }
        }

        dir.y = velY;

        velY -= Gravity * Time.deltaTime;

        gameObject.layer = LayerMask.NameToLayer("player_controller_no_collide");
        charC.Move(dir * Time.deltaTime);
        gameObject.layer = LayerMask.NameToLayer("player_controller");

        if (charC.isGrounded)
        {
            velY = 0;
        }
	}

    private InputSource<PlayerController> input = null;
    private InputManager.PlayerInfo playerInfo;
    public void SetInput(InputSource<PlayerController> newPlayerInput, InputManager.PlayerInfo playerInfo)
    {
        this.input = newPlayerInput;
        this.playerInfo = playerInfo;
    }
}