您的位置:首页 > 移动开发 > Unity3D

unity简单人物控制

2017-10-19 09:40 429 查看
public class PlayerMove : MonoBehaviour

{

    private CharacterController cc;

    [SerializeField]

    private float speed; //向前走的速度

    [SerializeField]

    private float jumpSpeedValue; //向上跳的速度值

    private Animator ani;

    private Vector3 JumpSpeed; //向上跳的速度

    private bool isJumping = false;  ///是否正在跳跃

    private float gravity = 3; //重力大小

    private ETCJoystick joyStick;

    private ETCButton jumpBtn;

    private void Awake()

    {

        speed = 4;

        JumpSpeed = Vector3.zero;

        jumpSpeedValue = 2;

        isJumping = false;

        gravity = 3;

        cc = this.GetComponent<CharacterController>();

        ani = this.GetComponent<Animator>();

    }

    void Start()

    {

        joyStick = ETCInput.GetControlJoystick("moveStick");

        jumpBtn = ETCInput.GetControlButton("jumpBtn");

        jumpBtn.onDown.AddListener(jumpBtnDown);

        // joyStick.onMove.AddListener(onMove);

    }

    private void jumpBtnDown()

    {

        if (cc.isGrounded) //判断人物是否已经落到地面。

        {

            //JumpSpeed = Vector3.up * jumpSpeedValue; //向上跳的初始速度。

            //isJumping = true;

            ani.SetTrigger("jump");

        }

    }

    //private void onMove(Vector2 v)

    //{

    //    print(string.Format("x:{0}   y:{1} ",v.x,v.y));

    //}

    void Update()

    {

        float h = joyStick.axisX.axisValue;

        float v = joyStick.axisY.axisValue;

        Vector3 sp = Vector3.zero;

        if (h != 0 || v != 0)

        {

     

            ani.SetBool("run", true);

            ani.SetFloat("runSpeed", speed / 2); ///同步动画播放的速度。

            ///速度和方向

            Vector3 dir = new Vector3(h, 0, v);

            dir = Camera.main.transform.TransformDirection(dir);

            dir.y = 0;

            dir.Normalize(); //长度为1.

            sp = dir * 4;

            //转身

            this.transform.LookAt(this.transform.position + sp);

        }

        else

        {

            ani.SetBool("run", false);

        }

        ///得到当前播放个的动画信息。

        ///正在播放run动画活着在空中时候可以发生位移

        AnimatorStateInfo stateinfo = ani.GetCurrentAnimatorStateInfo(0);

        if (stateinfo.IsName("Run") || (stateinfo.IsName("Jump") && !cc.isGrounded))

        {

            cc.SimpleMove(sp);

        }

        //if (Input.GetButtonDown("Jump"))

        //{

        //    if (cc.isGrounded) //判断人物是否已经落到地面。

        //    {

        //        JumpSpeed = Vector3.up * jumpSpeedValue; //向上跳的初始速度。

        //        isJumping = true;

        //        ani.SetTrigger("jump");

        //    }

        //}

        //if (isJumping) //正在跳跃

        //{

        //    //向上跳

        //    cc.Move(JumpSpeed * Time.deltaTime);

        //    //加上重力

        //    JumpSpeed += Time.deltaTime * Vector3.down * gravity;

        //    if(cc.isGrounded)

        //    {

        //        isJumping = false;

        //    }

        //}

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity