Unity3d TouchPhase。已结束,但未执行
在移动控制器中工作,我遇到了一个有趣的问题。当我快速按下左键和右键,然后跳跃时,角色会继续向右或向左移动,这让我认为在某些情况下,moveLeft或moveRight永远不会将设置设置为false 我读了很多关于TouchPhase的帖子。最后没有开火,你们有过这样的经历吗?有解决办法吗 编辑了我的帖子,包括我的代码员:Unity3d TouchPhase。已结束,但未执行,unity3d,unityscript,Unity3d,Unityscript,在移动控制器中工作,我遇到了一个有趣的问题。当我快速按下左键和右键,然后跳跃时,角色会继续向右或向左移动,这让我认为在某些情况下,moveLeft或moveRight永远不会将设置设置为false 我读了很多关于TouchPhase的帖子。最后没有开火,你们有过这样的经历吗?有解决办法吗 编辑了我的帖子,包括我的代码员: void Update(){ TouchManager (); } void FixedUpdate() { Move (); } void Move ()
void Update(){
TouchManager ();
}
void FixedUpdate() {
Move ();
}
void Move ()
{
//True if player hits ground
groundHit = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
//True if player hits top
topHit = Physics2D.OverlapCircle (topCheck.position, topRadius, whatIsTop);
anim.SetBool ("Ground", groundHit);
anim.SetFloat ("vSpeed", this.rigidbody2D.velocity.y);
if (Globals.gameStatus == Globals.GameStatus.Dead) {
anim.SetFloat ("Speed", 0);
return;
}
float move = 0.0f;
if (moveLeft) {
move = -1.0f;
}
if (moveRight) {
move = 1.0f;
}
anim.SetFloat ("Speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * maxSpeed, this.rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else
if (move < 0 && facingRight)
Flip ();
}
void TouchManager ()
{
if (Input.touchCount > 0) {
var touchCount = Input.touchCount;
for (var i = 0; i < touchCount; i++) {
var touch = Input.GetTouch (i);
//Debug.Log("FingerID: " + touch.fingerId.ToString());
if (touch.phase == TouchPhase.Began) {
//Debug.Log("Touch Began executed for touchID: " + touch.
if (guiLeft.HitTest (touch.position, Camera.main))
moveLeft = true;
if (guiRight.HitTest (touch.position, Camera.main))
moveRight = true;
if (guiJump.HitTest (touch.position, Camera.main))
{
Debug.Log("Jump pressed");
if (groundHit && !stickmanIsDead) {
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
}
//Record start time only once
holdJumpDownStartedAt = Time.time;
}
}
//Fall from Platforms
if(touch.phase == TouchPhase.Stationary){
if (guiJump.HitTest (touch.position, Camera.main)){
if(Time.time - holdJumpDownStartedAt > dropPlatformAt){
Debug.Log(string.Format("Holded for {0} second :) ",dropPlatformAt.ToString()));
holdJumpDownStartedAt = 0.0f;
#region Falling Keystroke
var playerFalling = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer ("Floor-Air"));
//Fall Key
if (playerFalling)
StartCoroutine (Fall ());
#endregion
}
}
}
if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled){
if (guiLeft.HitTest (touch.position, Camera.main))
moveLeft = false;
if (guiRight.HitTest (touch.position, Camera.main))
moveRight = false;
}
}
}
}
void Update(){
TouchManager();
}
void FixedUpdate(){
移动();
}
无效移动()
{
//如果玩家落地,则为True
groundHit=Physics2D.Overlap圆(groundCheck.position、groundRadius、whatIsGround);
//如果玩家点击顶部,则为True
topHit=Physics2D.Overlap圆(topCheck.position,topRadius,whatIsTop);
anim.SetBool(“地面”,地面打击);
anim.SetFloat(“vSpeed”,this.rigiidbody2d.velocity.y);
if(Globals.gameStatus==Globals.gameStatus.Dead){
anim.SetFloat(“速度”,0);
返回;
}
浮动移动=0.0f;
如果(向左移动){
移动=-1.0f;
}
如果(向右移动){
move=1.0f;
}
动画设置浮动(“速度”,数学Abs(移动));
rigidbody2D.velocity=新矢量2(move*maxSpeed,this.rigidbody2D.velocity.y);
如果(移动>0&!朝向右侧)
翻转();
其他的
如果(移动<0&&朝向右侧)
翻转();
}
void TouchManager()
{
如果(Input.touchCount>0){
var touchCount=Input.touchCount;
对于(变量i=0;idropPlatformAt){
Log(string.Format(“保持{0}秒:)”,dropPlatformAt.ToString());
保持跳线下降起始温度=0.0f;
#区域下降击键
var playerFalling=Physics2D.Linecast(transform.position,groundCheck.position,1搞定了,伙计们,无论如何,谢谢,我最后做的是:
void CharacterTouchManager(){
//Debug.Log(“移动…”+rigidbody2D.velocity.x.ToString());
//Log(“播放器是:+Globals.playIsDoing.ToString());
//运动
如果(Input.touchCount>0){
var touchCount=Input.touchCount;
对于(变量i=0;i-0.1){
//moveLeft=true;
//}
//否则
//moveLeft=true;
moveLeft=true;
moveRight=false;
}
//如果玩家按下屏幕的1/5到2/5之间,向右移动。
if(touch.position.x>Screen.width/5&&touch.position.x0){
var touchCount=Input.touchCount;
对于(变量i=0;idropPlatformAt){
保持跳线下降起始温度=0.0f;
#区域下降击键
var playerFalling=Physics2D.Linecast(transform.position,groundCheck.position,1)您能给我们看一下您的特殊代码吗?确实有一些情况下TouchPhase.Ended据报道无法触发(什么是
//Debug.Log("Moving..." + rigidbody2D.velocity.x.ToString());
//Debug.Log("Player is: " + Globals.playIsDoing.ToString());
//Movement
if(Input.touchCount > 0){
var touchCount = Input.touchCount;
for (var i = 0; i < touchCount; i++) {
var touch = Input.GetTouch (i);
//if player presses less than 1/5 of the screen, go left.
if(touch.position.x < Screen.width / 5 && touch.position.y < Screen.height / 3){
touchControlSwitchToJumpMode = false;
//if(rigidbody2D.velocity.x > -0.1){
// moveLeft = true;
//}
//else
// moveLeft = true;
moveLeft = true;
moveRight = false;
}
//if player presses between 1/5 and 2/5 of the screen, go right.
if(touch.position.x > Screen.width / 5 && touch.position.x < Screen.width / 5 * 2 && touch.position.y < Screen.height / 3){
touchControlSwitchToJumpMode = false;
//if(rigidbody2D.velocity.x < 0.1){
// moveRight = true;
//}
moveRight = true;
moveLeft = false;
}
}
}
else {
moveLeft = moveRight = false;
}
//Jumping and Dropping from platforms
if (Input.touchCount > 0) {
var touchCount = Input.touchCount;
for (var i = 0; i < touchCount; i++) {
var touch = Input.GetTouch (i);
//Make sure we're not touching jump by itself to avoid character running continously
if(!touchControlSwitchToJumpMode && touchCount == 1 && guiJump.HitTest (touch.position, Camera.main)){
moveRight = moveLeft = false;
touchControlSwitchToJumpMode = true;
}
if (touch.phase == TouchPhase.Began) {
if (guiJump.HitTest (touch.position, Camera.main))
{
if (groundHit && !stickmanIsDead) {
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
}
//Record start time only once
holdJumpDownStartedAt = Time.time;
}
}
if(touch.phase == TouchPhase.Stationary){
if (guiJump.HitTest (touch.position, Camera.main)){
if(Time.time - holdJumpDownStartedAt > dropPlatformAt){
holdJumpDownStartedAt = 0.0f;
#region Falling Keystroke
var playerFalling = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer ("Floor-Air"));
//Fall Key
if (playerFalling)
StartCoroutine (Fall ());
#endregion
}
}
}
}
}
}