【问题标题】:Player Momentarily Stuck at Edges of Screen! Unity玩家瞬间卡在屏幕边缘!统一
【发布时间】:2020-10-14 20:45:28
【问题描述】:

我正在开发一个 Unity 2d 游戏,该游戏围绕一个玩家在飞行时躲避随机物体......类似于 flappybird。游戏运行正常。

但问题是当玩家触摸顶部屏幕边缘并继续施加力时,玩家会卡在边缘一段时间。我想我需要在玩家触摸屏幕边缘时禁用增加力。 我希望你们帮助我。 这是播放器代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Jetpack: MonoBehaviour
{
    public GameManager GameManager;
    private Rigidbody2D rb;
    private float jumpForce = 40f;
    private bool engineIsOn;

    [SerializeField] private GameObject fire;

    void Start()
    {
        engineIsOn = false;
        fire.SetActive(false);
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            engineIsOn = true;
            fire.SetActive(true);
        }
        else
        {
            engineIsOn = false;
            fire.SetActive(false);
        }


        transform.position = new Vector3(Mathf.Clamp(transform.position.x, -4.5f, 4.5f),
        Mathf.Clamp(transform.position.y, -4.5f, 4.5f), transform.position.z);

        
    }
    private void FixedUpdate()
    {
        switch (engineIsOn)
        {
            case true:
                rb.AddForce(new Vector2(0f, jumpForce),ForceMode2D.Force);
                break;

            case false:
                rb.AddForce(new Vector2(0f, 0f), ForceMode2D.Force);
                break;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {

        GameManager.gameover();
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    在处理Rigidbody 时,不要通过Transform 组件设置任何值!

    这可能会破坏物理学。

    然后基本上你已经说过了:如果你在边缘,不要增加更多的向上力量。

    作为一个简单的技巧,您可以将 Y 速度设置为 0,例如在撞击边缘时

    private void FixedUpdate()
    {
        // Very unusual to use a switch for a single bool
        if (engineIsOn)
        {
            rb.AddForce(new Vector2(0f, jumpForce),ForceMode2D.Force);
        }
        // This does absolutely nothing
        // Adding a force of 0,0 doesn't change the velocity at all ...
        //else
        //{
        //    rb.AddForce(new Vector2(0f, 0f), ForceMode2D.Force);
        //}
    
        // Check if currently hitting an edge
        // and if so set the velocity on that axis to 0
        // also clamp the position then
        var velocity = rb.velocity;
        var position = rb.position;
        if(Mathf.Abs(rb.position.x) >= 4.5f)
        {
            velocity.x = 0;
            position.x = Mathf.Clamp(position.x, -4.5f, 4.5f);
        }
        if(Mathf.Abs(rb.position.y) >= 4.5f) 
        {
            velocity.y = 0;
            position.y = Mathf.Clamp(position.y, -4.5f, 4.5f);
        }
        rb.velocity = velocity;
        rb.position = position;
    }
    

    【讨论】:

    • 你在处理rigitbody时说过transform组件。所以你建议我应该用什么来让玩家不要离开屏幕边缘?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多