当我改变Unity中的位置时,我的球员会发抖

时间:2018-05-19 10:18:15

标签: c# unity3d

嘿伙计们我的问题在标题中。基本上当我按下播放一切正常时,球开始上升而不会摇晃,但是当我开始滑动以改变位置时它会开始颤抖。

我已经尝试了不同的内容,例如更改为FixedUpdate()或重置播放器,但它并没有改变。顺便说一下球上没有动画。你能帮助我吗?

以下是带滑动参数的脚本:

public class Swipe : MonoBehaviour 
{
private const float DEADZONE = 100.0f;

public static Swipe Instance { set; get; }

private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private Vector2 startTouch, swipeDelta;

public bool Tap { get { return tap; } }
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }

private void Awake()
{
    Instance = this;
}

private void Update()
{
    // Reseting all the booleans
    tap = swipeLeft = swipeRight = swipeDown = swipeUp = false;

    #region Stadalone Inputs
    if (Input.GetMouseButtonDown(0))
    {
        tap = true;
        startTouch = Input.mousePosition;
    }
    else if (Input.GetMouseButtonUp(0))
    {
        startTouch = swipeDelta = Vector2.zero;
    }
    #endregion

    #region Mobile Inputs
    if (Input.touches.Length != 0)
    {
        if (Input.touches[0].phase == TouchPhase.Began)
        {
            tap = true;
            startTouch = Input.mousePosition;
        }
        else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
        {
            startTouch = swipeDelta = Vector2.zero;
        }
    }
    #endregion

    // Calculate The Distance
    swipeDelta = Vector2.zero;
    if (startTouch != Vector2.zero)
    {
        if (Input.touches.Length != 0)
        {
            swipeDelta = Input.touches[0].position - startTouch;
        }
        else if (Input.GetMouseButton(0))
        {
            swipeDelta = (Vector2)Input.mousePosition - startTouch;
        }
    } 

    // Did we cross the deadzone ?
    if (swipeDelta.magnitude > DEADZONE)
    {
        // Which direction ?
        float x = swipeDelta.x;
        float y = swipeDelta.y;

        if (Mathf.Abs(x) > Mathf.Abs(y))
        {
            // Left or Right
            if (x < 0)
                swipeLeft = true;
            else
                swipeRight = true;
        }
        else
        {
            // Up or Down
            if (y < 0)
                swipeDown = true;
            else
                swipeUp = true;
        }

        startTouch = swipeDelta = Vector2.zero;
    }
}   
}

这是Update()或(FixedUpdate()中的脚本部分,它不会改变任何东西),用于使其在播放器上移动一侧或另一侧:

 // Gather the inputs in which tube we should be
    if (Swipe.Instance.SwipeLeft)
        MoveTube(false);
    if (Swipe.Instance.SwipeRight)
        MoveTube(true);


    // Calculate where we should be in the future
    Vector3 targetPosition = transform.position.y * Vector3.up;
    if (desiredTube == 0)
        targetPosition += Vector3.left * TUBE_DISTANCE;
    else if (desiredTube == 2)
        targetPosition += Vector3.right * TUBE_DISTANCE; 

    // Let's calculate our move delta
    Vector3 moveVector = Vector3.zero;
    moveVector.x = (targetPosition - transform.position).normalized.x * speed;
    moveVector.y = speed;

    // Move the ball
    controller.Move(moveVector * Time.deltaTime);

}

private void MoveTube(bool goingRight)
{
    desiredTube += (goingRight) ? 1 : -1;
    desiredTube = Mathf.Clamp(desiredTube, 0, 2);
}

2 个答案:

答案 0 :(得分:3)

我相信你可能会遇到问题:

// Gather the inputs in which tube we should be
    if (Swipe.Instance.SwipeLeft)
        MoveTube(false);
    if (Swipe.Instance.SwipeRight)
        MoveTube(true);

由于每帧调用Update()一次,这意味着每秒将检查几次。所以它可能每秒都会改变玩家的位置几次,这就是你提到的震撼效果。

您可以尝试限制玩家每秒可以滑动的时间。

private float SwipeRate = 0.1f;
private float NextSwipe = 0.0f;

Update(){

    if (Swipe.Instance.SwipeLeft && Time.time > NextSwipe)
    {
        NextSwipe = Time.time+SwipeRate;
        MoveTube(false);
    }
    if (Swipe.Instance.SwipeRight && Time.time > NextSwipe)
    {
        NextSwipe = Time.time+SwipeRate;
        MoveTube(true);
    }

}

修改

private void MoveTube(bool goingRight)
{
    desiredTube += (goingRight) ? 1 : -1;
    desiredTube = Mathf.Clamp(desiredTube, 0, 2);

// Calculate where we should be in the future
    Vector3 targetPosition = transform.position.y * Vector3.up;
    if (desiredTube == 0)
        targetPosition += Vector3.left * TUBE_DISTANCE;
    else if (desiredTube == 2)
        targetPosition += Vector3.right * TUBE_DISTANCE; 

    // Let's calculate our move delta
    Vector3 moveVector = Vector3.zero;
    moveVector.x = (targetPosition - transform.position).normalized.x * speed;
    moveVector.y = speed;

    // Move the ball
    controller.Move(moveVector * Time.deltaTime);
}

答案 1 :(得分:0)

我找到了如何解决问题。所以基本上我使用相同的速度变量来移动我的球在x和y轴上,似乎它为团结创造了一些“切换问题”。所以我只为速度创建了两个不同的变量,我解决了这样的错误。