簡體   English   中英

2d 平台游戲石頭改變大小

[英]2d Platformer game stone changing size

我正在嘗試制作平台游戲。 在平台游戲中,有一個移動平台,當它接觸到它的變換的孩子時,它會使其接觸到它的所有東西。 有趣的是,每當一塊石頭(帶有剛體和多邊形對撞機的可移動 object)接觸移動平台時,石頭的鱗片就會失控。 即使變換組件上的刻度讀數相同,但在觸摸它時它似乎比實際大小更大或更小。 當它停止接觸平台時,石頭看起來很正常。 誰能幫我。 謝謝你。

這是移動平台的移動平台腳本。

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

public class MoveTwoTransforms : MonoBehaviour
{
    public Transform pointA;
    public Transform pointB;
    public bool HasReachedA;
    public bool HasReacedB;

    // Start is called before the first frame update
    void Start()
    {
        transform.position = pointA.position;
        HasReacedB = true;
        HasReachedA = false;

        StartCoroutine(GlideAround());
    }

    // Update is called once per frame
    void Update()
    {

    }

    public IEnumerator GlideAround()
    {
        while (true)
        {
            while (HasReachedA == false)
            {
                yield return new WaitForEndOfFrame();

                transform.position = Vector2.Lerp(transform.position, pointA.position, 0.01f);
                if ((Mathf.Abs(Vector2.Distance(pointA.position, transform.position)) < 0.01f))
                {
                    HasReacedB = false;
                    HasReachedA = true;
                }
            }

            while (HasReacedB == false)
            {
                yield return new WaitForEndOfFrame();
                transform.position = Vector2.Lerp(transform.position, pointB.position, 0.01f);

                if ((Mathf.Abs(Vector2.Distance(pointB.position, transform.position)) < 0.01f))
                {
                    HasReacedB = true;
                    HasReachedA = false;
                }
            }
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if((collision.gameObject.tag == "Stone"|| collision.gameObject.tag == "Player") && (collision.transform.position.y - collision.transform.lossyScale.y / 2 >= transform.position.y))
        {
            collision.transform.parent = transform;
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        collision.transform.parent = null;
    }
}

這是石頭劇本

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

public class RememberPositions : MonoBehaviour
{
    public Vector3 StartingPosition;
    public Vector3 StartingRotation;
    public Vector3 StartingScale;

    float StartRotation;

    // Start is called before the first frame update
    void Start()
    {
        StartingPosition = new Vector3(transform.position.x, transform.position.y, 0);
        StartingRotation = new Vector3(0, 0, transform.position.z);
        StartingScale = new Vector3(transform.localScale.x, transform.localScale.y, transform.localScale.z);
    }

    // Update is called once per frame
    void Update()
    {
        transform.localScale = StartingScale;
    }
}

沒有任何錯誤,剛體似乎正在改變為石頭的形狀。 誰能指定我應該使用的正確代碼? 謝謝你。

如果你的移動平台被縮放,你的石頭也會被縮放。 此處顯示了一些解決方法來避免此問題。 其中之一是將子GameObject對象添加到平台,並將碰撞處理從平台移動到該子對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM