首页 文章

Unity动画c#代码

提问于
浏览
0

我正在制作一个类似于涂鸦跳跃的游戏,3D,我想当玩家触摸动画将播放的障碍时,这意味着动画必须播放,我认为它不会点击任何按钮以便动画播放,玩家自动跳跃,所以在碰到障碍物之后它继续循环和循环并且它不会停止,请帮助我这里是左右移动的代码,跳转,这里我包括动画:

using UnityEngine;
using System.Collections;

public class LeftRightMovement : MonoBehaviour {
    public float jump = 15f;
    float speed = 4f;
    float movevelocity;
    static Animator anim;
    void Start()
    {
        anim = GetComponent<Animator> ();
        anim.SetBool ("isJumping",false);
    }
    void FixedUpdate () 
    {
         if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)) 
            {
                this.gameObject.transform.Translate (Vector3.left * Time.deltaTime * -speed);                                       
            } 
            else if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) 
            {
                this.gameObject.transform.Translate (Vector3.right * Time.deltaTime * -speed);
            }

    }

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Obstacle") {
            GetComponent<Rigidbody> ().velocity = new Vector3 (GetComponent<Rigidbody> ().velocity.x, jump, 0);
                anim.SetBool ("isJumping",true);
        } else if (col.gameObject.tag == "Platform") {
            GetComponent<Rigidbody> ().velocity = new Vector3 (GetComponent<Rigidbody> ().velocity.x, jump, 0);
            anim.SetBool ("isJumping",true);
        } else if (col.gameObject.tag == "Monster") {
            GetComponent<Rigidbody> ().velocity = new Vector3 (GetComponent<Rigidbody> ().velocity.x, jump, 0);
            anim.SetBool ("isJumping",true);
        } else if (col.gameObject.tag == "Virtaliot") {
            GetComponent<Rigidbody> ().velocity = new Vector3 (GetComponent<Rigidbody> ().velocity.x, jump * 3, 0);
            anim.SetBool ("isJumping",true);
        } else if (col.gameObject.tag == null) {
            anim.SetBool ("isJumping",false);
        }
    }
}

请帮我!

3 回答

  • 0

    在动画制作机器中,单击“跳转”状态 . 然后单击跳转动画(右上角) . 在检查器中,您应该看到动画的参数 . 取消勾选“循环时间”复选框 . 当你到达Jump状态时,这将使你的动画只播放一次 .

  • 0

    在您的代码中,当与障碍物碰撞时,您将isJumping设置为true,但是您永远不会将其设置为false,因此它永远不会停止 .

    你还应该实例化一个刚体变量:

    Rigidbody rigidbody = GetComponent<Rigidbody>();
    

    因此,您可以在每次需要时使用GetComponent重复使用它 .

    希望对你有所帮助 .

  • 0

    您应该创建一个oncollisionexit函数来停止动画:

    Using UnityEngine;
    using System.Collections;
    
    public class Example : MonoBehaviour{
    
     void OnCollisionExit (Collision collisioninfo){
    
        GetComponent<Animation> ().Stop ("Jumping");
    
         }
    }
    

相关问题