我正在制作类似于涂鸦跳跃的游戏,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);
}
}
}
请帮助我!
答案 0 :(得分:0)
在您的代码中,当与障碍物碰撞时,您将isJumping设置为true,但是您永远不会将其设置为false,因此它永远不会停止。
此外,您应该像这样实例化一个刚体变量:
Rigidbody rigidbody = GetComponent<Rigidbody>();
因此,您可以在每次需要时使用GetComponent重复使用它。
希望对你有所帮助。
答案 1 :(得分:0)
在动画师状态机中,单击“跳转”状态。然后单击跳转动画(右上角)。 在检查器中,您应该看到动画的参数。取消勾选“循环时间”复选框。当你到达跳跃状态时,这将使你的动画只播放一次。
答案 2 :(得分:0)
你应该制作一个oncollisionexit函数来停止动画:
Using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour{
void OnCollisionExit (Collision collisioninfo){
GetComponent<Animation> ().Stop ("Jumping");
}
}