繁体   English   中英

Unity Android 2D运动问题

[英]Unity android 2d movement issue

我想创建一个游戏,在其中可以左右移动盒子以避免掉落盒子,并且它可以工作,但是问题是当我将手指放在一个位置时,玩家盒子就会左右晃动。 您可以在此gif http://gfycat.com/FragrantBrokenEyas上看到它。 这是C#脚本的代码

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
private Camera m_cam;
private float m_offsetRight = 1.0F;
private float m_offsetLeft = 0.0F;


void Awake() {
    m_cam = Camera.main;
}


void Update () {
    rigidbody2D.velocity = new Vector2(0 * 10, 0);
    Move ();
}


void Move () {
    if (Application.platform == RuntimePlatform.Android) {
        Vector3 player_pos = m_cam.WorldToViewportPoint(rigidbody2D.position);
        Vector3 touch_pos = new Vector3(Input.GetTouch(0).position.x, 0, 0);

        touch_pos = Camera.main.ScreenToViewportPoint(touch_pos);

        if(touch_pos.x > player_pos.x)
            rigidbody2D.velocity = new Vector2(1 * 10, 0);
        else if (touch_pos.x < player_pos.x)
            rigidbody2D.velocity = new Vector2(-1 * 10, 0);
    }
    else{       
        Vector3 player_pos = m_cam.WorldToViewportPoint(rigidbody2D.position);
        float h = Input.GetAxis ("Horizontal");

        if (h > 0 && player_pos.x < m_offsetRight)
            rigidbody2D.velocity = new Vector2(h * 10, 0);
        else if (h < 0 && player_pos.x > m_offsetLeft)
            rigidbody2D.velocity = new Vector2(h * 10, 0);
        else 
            rigidbody2D.velocity = new Vector2(0, rigidbody2D.velocity.y);
    }
}


void OnTriggerEnter2D(Collider2D other) {
    if (other.gameObject.name == "Enemy(Clone)") {
        Destroy(other.gameObject);
        GameSetUp.score -= 2;
    } else if (other.gameObject.name == "Coin(Clone)") {
        GameSetUp.score += 2;
        Destroy(other.gameObject);
    }
}

}

我认为问题出在这里:

touch_pos = Camera.main.ScreenToViewportPoint(touch_pos);

        if(touch_pos.x > player_pos.x)
            rigidbody2D.velocity = new Vector2(1 * 10, 0);
        else if (touch_pos.x < player_pos.x)
            rigidbody2D.velocity = new Vector2(-1 * 10, 0);

该“一个位置”可能是触摸的x坐标与播放器框的x坐标大致相同的位置。

当手指按下该区域时,在第一帧上touch_pos.x > player_pos.x为true,速度设置为正。 在下一帧上,播放器已朝正方向移动,这次touch_pos.x < player_pos.x为true,速度设置为负。 这导致震动效果。

为了避免这种情况,您可以例如缓慢增加和降低速度,而不是在一帧中完全将其更改为相反的速度。 您可以通过将代码更改为此:

    touch_pos = Camera.main.ScreenToViewportPoint(touch_pos);

    const float acceleration = 60.0f;
    const float maxSpeed = 10.0f;

    if(touch_pos.x > player_pos.x)
        if(rigidbody2D.velocity.x < maxSpeed)
            rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x + acceleration * Time.deltaTime, 0);
        else
            rigidbody2D.velocity = new Vector2(maxSpeed, 0);
    else if (touch_pos.x < player_pos.x)
        if(rigidbody2D.velocity.x > -maxSpeed)
            rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x - acceleration * Time.deltaTime , 0);
        else
            rigidbody2D.velocity = new Vector2(-maxSpeed, 0);

只需将acceleration调整为看起来正确的值即可。

暂无
暂无

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

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