0
私は簡単な無限のランナーゲームを作成しています。地面は異なるコライダーでできています。アニメーションの状態がキャラクタの動作に一致しません(Unity、C#)
- を実行し、
- のGet力、
- ジャンプ、
- ビッグジャンプ:アニメーションの4つの状態があります。
すべての状態がループされ、終了時間はありません。
残念ながら、うまくいきません。最大の問題は、スペースバーをすばやく押すと、キャラクターは空中で状態0(アニメーション実行中)になります。時には、空中でも、キャラクターは力を得ることができます。キャラクターがジャンプしているときでさえ、それは別のコライダーに触れることができると私は思う。しかし、私の予防方法は機能しません。これらのアニメーション状態をキャラクタビヘイビアと一致させるにはどうすればよいですか?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Move : MonoBehaviour {
public Animator animator;
Rigidbody rigidBody;
CapsuleCollider capsuleCollider;
int isGrounded=2;
int jumpType;
float force = 2;
float jump = 20000;
float minJump = 0.1f;
float jumpTime;
bool isJumping;
void Start()
{
animator = GetComponentInChildren<Animator>();
rigidBody=GetComponent<Rigidbody>();
capsuleCollider = GetComponent<CapsuleCollider>();
}
void Update()
{
if(isJumping)
{
jumpTime+=Time.deltaTime;
}
// if the character is grounded
if (isGrounded==1)
{
//jump if the key is released, jump
if(Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
{ isJumping=true;
isGrounded=2;
if(rigidBody.velocity.y<30)
{
rigidBody.AddForce(Vector3.up*jump*force*0.55f);
jumpType=2;
if (force>2.5f) jumpType =3;
animator.SetInteger("state",jumpType);
force=2;
}
}
//if the key is pressed, add force. longer you hold, higher you jump
else if(Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
{
animator.SetInteger("state",1);
force+=Time.deltaTime*0.7f;
}
//just keep running
else if(rigidBody.velocity.y<0)
{
animator.SetInteger("state",0);
}
}
}
void OnCollisionEnter(Collision collision)
{
//you are grounded. additional 'if', because sometimes you touch collider after jump
if(isJumping && isGrounded==2)
{ if(jumpTime>minJump)
{
isGrounded=1;
isJumping=false;
jumpTime=0;
}}
}
}
これは、あなたがランナーゲームを書いている第10人のように、試験になるはずです。面白いところは、まったく同じ場所で同じ過ちをすることです。ここにあなたの答えです:http://stackoverflow.com/questions/37888210/how-to-apply-jump-both-to-controller-and-player-for-an-endless-runner-3d-in-unit –
私の問題ジャンプの不足ではなく、アニメーションとのシンクロの欠如です。残りは問題ありません。 – Mike
まったく。そしてそれには理由があります。統一はあなたがすることをあなたがすることではなく、あなたがしたいことではありません。 –