简体   繁体   English

Unity动画C#代码

[英]Unity animation c# code

I am making a game similar to doodle jump, 3d, I want when the player touches an obstacle the animation will play, that means that oncollisionenter the animation must play, I think it doesn't work because I don't click on any button so the animation will play, the player jumps automatically, so after it touches once the obstacle it continues looping and looping and it doesn't stop, please help me here is the code for left right movement , jump, and here I included animation: 我正在制作类似于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);
        }
    }
}

please help me! 请帮我!

In your code you set isJumping to true when colliding with an obstacle but you never set it back to false so it never stops. 在您的代码中,与障碍物碰撞时将isJumping设置为true,但是您从未将其设置为false,因此它永远不会停止。

Also you should instantiate a rigidbody variable like that: 同样,您应该像这样实例化刚体变量:

Rigidbody rigidbody = GetComponent<Rigidbody>();

So you can reuse it with using GetComponent each time you need it. 因此,您可以在每次需要时使用GetComponent来重用它。

Hope that helps you. 希望对您有帮助。

In your animator state machine, click on the "Jump" state. 在动画制作器状态机中,单击“跳转”状态。 Then click on the jump animation (upper right). 然后单击跳转动画(右上方)。 In the inspector, you should see the parameters of your animation. 在检查器中,您应该看到动画的参数。 Untick the "loop time" checkbox. 取消勾选“循环时间”复选框。 This will make your anim play only once when you arrive in the Jump state. 当您进入“跳跃”状态时,这只会使您的动画只播放一次。

You should make an oncollisionexit function where you will stop the animation: 您应该创建一个oncollisionexit函数,在其中将停止动画:

Using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour{

 void OnCollisionExit (Collision collisioninfo){

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

     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM