简体   繁体   English

2d 平台游戏石头改变大小

[英]2d Platformer game stone changing size

I am trying to make a platformer game.我正在尝试制作平台游戏。 In the platformer game has a moving platform that makes everything it touches its transform's child while it touches it.在平台游戏中,有一个移动平台,当它接触到它的变换的孩子时,它会使其接触到它的所有东西。 Interestingly, whenever a stone(A movable object with a rigidbody and a polygon collider) touches the moving platform, the stone's scale goes haywire.有趣的是,每当一块石头(带有刚体和多边形对撞机的可移动 object)接触移动平台时,石头的鳞片就会失控。 Even though the scale reads the same on the transform component, it appears to be larger or smaller than it really is when touching it.即使变换组件上的刻度读数相同,但在触摸它时它似乎比实际大小更大或更小。 When it stops touching the platform, the stone appears normal.当它停止接触平台时,石头看起来很正常。 Can anyone help me.谁能帮我。 Thank you.谢谢你。

This is the moving platform script that moves the platform around.这是移动平台的移动平台脚本。

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;
    }
}

This is the stone script这是石头剧本

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;
    }
}

There are no errors whatsoever and the rigidbody seems to be changing to the shape of the stone.没有任何错误,刚体似乎正在改变为石头的形状。 Can anyone please specify the correct code I should use?谁能指定我应该使用的正确代码? Thank you.谢谢你。

If your moving platform is scaled, your stone will get scaled too.如果你的移动平台被缩放,你的石头也会被缩放。 There are workarounds shown here to avoid this issue. 此处显示了一些解决方法来避免此问题。 One of them is adding a child GameObject to the platform, and moving the collision handling from the platform to that child.其中之一是将子GameObject对象添加到平台,并将碰撞处理从平台移动到该子对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM