Unity跳转脚本isGrounded

时间:2017-07-20 23:48:46

标签: c# animation unity3d

你好所以我有这个c#控制器一切都很好,但我可以说我在空中高高的立方体上,如果我只是走了它不跳它不会做空气动画,它仍然认为它的接地但它的没有什么可以碰撞的。你走出平台的主要问题是它仍然认为它已经停飞了 这是代码;

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerController : MonoBehaviour
{
    public float moveSpeed = 10f;
    public float turnSpeed = 50f;
    Animator anim;
    public bool isGrounded;

    private Rigidbody rb;

    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();
        isGrounded = true;
        rb = GetComponent<Rigidbody>();

    }

    // Update is called once per frame
    void Update()
    {

        anim.SetFloat("VelocityX", 0);
        anim.SetFloat("VelocityY", 0);
        if ((Input.GetKey(KeyCode.S)) && (Input.GetKey(KeyCode.Q) == false) && (Input.GetKey(KeyCode.E) == false))
        {

            transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
            anim.SetFloat("VelocityX", 0);
            anim.SetFloat("VelocityY", -1);
            moveSpeed = 1f;
        }


        if ((Input.GetKey(KeyCode.W)) && (Input.GetKey(KeyCode.S) == false))
        {
            anim.SetFloat("VelocityX", 0);
            anim.SetFloat("VelocityY", 1);
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
            moveSpeed = 4f;
        }


        if (Input.GetKey(KeyCode.A))
        {
            anim.SetFloat("VelocityX", -1);
            anim.SetFloat("VelocityY", 0);
            transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.D))
        {
            anim.SetFloat("VelocityX", 1);
            anim.SetFloat("VelocityY", 0);
            transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
        }


        if (Input.GetKey(KeyCode.Q))
        {
            anim.SetFloat("VelocityX", -0.5f);
            anim.SetFloat("VelocityY", 1);
            moveSpeed = 4f;

            transform.Translate(-Vector3.right * moveSpeed * Time.deltaTime);

        }
        if (Input.GetKey(KeyCode.E))
        {

            anim.SetFloat("VelocityX", 0.5f);
            anim.SetFloat("VelocityY", 1);
            transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
            moveSpeed = 4f;
        }

        if (isGrounded)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                anim.SetBool("isGrounded", false);
                rb.velocity = new Vector3(0f, 5f, 0f);
                isGrounded = false;
                anim.SetFloat("JumpX", 0.21f);
                anim.SetFloat("JumpY", 0);

            }
        }
            if (isGrounded == true)
            {
                isGrounded = true;
                anim.SetBool("isGrounded", true);

            }


            if (isGrounded == false)
            {
                anim.SetFloat("JumpX", -9);
                anim.SetFloat("JumpY", 0);
                anim.SetBool("isGrounded", false);
            }


            if ((Input.GetKey(KeyCode.W)) && (Input.GetKey(KeyCode.D)))
            {
                moveSpeed = 4f;
                anim.SetFloat("VelocityX", 0.1665958f);
                anim.SetFloat("VelocityY", 1.281064f);
            }
            if ((Input.GetKey(KeyCode.W)) && (Input.GetKey(KeyCode.A)))
            {
                moveSpeed = 4f;
                anim.SetFloat("VelocityX", -0.1665958f);
                anim.SetFloat("VelocityY", 1.281064f);
            }
            if ((Input.GetKey(KeyCode.W)) && (Input.GetKey(KeyCode.Q)))
            {
                moveSpeed = 4f;
                anim.SetFloat("VelocityX", -0.2470213f);
                anim.SetFloat("VelocityY", 1.281064f);
            }
            if ((Input.GetKey(KeyCode.W)) && (Input.GetKey(KeyCode.E)))
            {
                moveSpeed = 4f;
                anim.SetFloat("VelocityX", 0.1665958f);
                anim.SetFloat("VelocityY", 1.281064f);
            }
            if ((Input.GetKey(KeyCode.S)) && (Input.GetKey(KeyCode.A)))
            {
                anim.SetFloat("VelocityX", -1.154681f);
                anim.SetFloat("VelocityY", -1.005319f);
                moveSpeed = 1f;
            }
            if ((Input.GetKey(KeyCode.S)) && (Input.GetKey(KeyCode.D)))
            {
                anim.SetFloat("VelocityX", 1.154681f);
                anim.SetFloat("VelocityY", -1.005319f);
                moveSpeed = 1f;
            }


        }





    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Ground"))
        {
            anim.SetBool("isGrounded", true);
            isGrounded = true;

        }

        if (other.gameObject.CompareTag("Ground") == false)
        {
            isGrounded = false;
        }



    }


}

0 个答案:

没有答案