繁体   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