Unity动画c#代码

问题描述 投票:0回答:3

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

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);
        }
    }
}

请帮我!

c# animation unity3d
3个回答
0
投票

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

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

Rigidbody rigidbody = GetComponent<Rigidbody>();

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

希望对你有所帮助。


0
投票

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


0
投票

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

Using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour{

 void OnCollisionExit (Collision collisioninfo){

    GetComponent<Animation> ().Stop ("Jumping");

     }
}
© www.soinside.com 2019 - 2024. All rights reserved.