unity3d C#将父控件添加到移动平台后,我的播放器(白色框)在与地面接触时抖动

ff29svar  于 2023-01-05  发布在  C#
关注(0)|答案(1)|浏览(108)

在离开对撞机和接触地面后,我的角色在地面上抖动。项目是2d的
我为这个问题的发生添加了以下代码行。

private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "MovePtl")
        {
            transform.SetParent(other.transform);
        }
    }
    private void OnTriggerExit(Collider2D other)
    {
        if(other.tag == "MovePtl")
        {
            transform.SetParent(null);
        }
    }

这是播放器的完整脚本

void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);

        isGrounded = Physics2D.OverlapArea(groundPoints[0].position, groundPoints[1].position, groundMask);

        if (isGrounded == true)
        {

            if (currentJumps != 0)
            {
                currentJumps = 0;
            }

            if (Input.GetButtonDown("Jump"))
            {
                currentJumps++;
                rb.velocity = new Vector2(rb.velocity.x, 0);
                rb.AddForce(Vector2.up * jumpForce);
            }
        }
        else
        {
            if (Input.GetButtonDown("Jump") && currentJumps < totalJumps)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
                rb.AddForce(Vector2.up * jumpForce);
                currentJumps++;
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "MovePtl")
        {
            transform.SetParent(other.transform);
        }
    }
    private void OnTriggerExit(Collider2D other)
    {
        if(other.tag == "MovePtl")
        {
            transform.SetParent(null);
        }
    }
}

这是移动平台的完整脚本

public class MovePlatform : MonoBehaviour
{
    public Transform platform;
    public Transform parentPoints;
    List<Transform> points = new List<Transform>();
    int currentPoint;
    public float speed;
    // Start is called before the first frame update
    void Start()
    {
        currentPoint = 0;
        for (int i = 0; i < parentPoints.childCount; i++)  // for es un loop donde se declara un iterador que debe cumplir una condicion y cada vez que se cumple el loop el iterador cambiaI = iterador,
        {
            points.Add(parentPoints.GetChild(i));
        }
        
    }

    // Update is called once per frame
    void Update()
    {
        platform.position = Vector2.MoveTowards(platform.position, points[currentPoint].position, speed * Time.deltaTime);
        if(platform.position == points[currentPoint].position)
        {
            currentPoint++;
            if(currentPoint >= points.Count)
            {
                currentPoint = 0;
            }
        }
    }
}

平台总共有2个点,移动平台的碰撞器在平台的顶部,玩家清楚地退出碰撞器,玩家碰撞器是相同的,但在玩家下面
我想停止之后发生的抖动不知道这是一个编程问题还是一个统一的问题。项目中没有艺术,只有框精灵

ffscu2ro

ffscu2ro1#

将刚体2D上的“碰撞检测”模式设置为“连续”而不是“离散”。

相关问题