【发布时间】:2021-11-18 13:29:45
【问题描述】:
基本上,我遇到的问题是平台生成垂直,但是它们会移到右上角(见图)。因此玩家无法跳到平台,而我必须提高他们的速度和跳跃值。我想让平台可以在随机范围内生成,也可以跳跃。
平台生成代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformGenerator : MonoBehaviour
{
public GameObject thePlatform;
public Transform generationPoint;
public float distanceBetween;
private float platformWidth;
public float distanceBetweenMin;
public float distanceBetweenMax;
// Start is called before the first frame update
void Start()
{
platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;
}
// Update is called once per frame
void Update()
{
if(transform.position.y < generationPoint.position.y)
{
distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);
transform.position = new Vector3(transform.position.y + platformWidth + distanceBetween, transform.position.x, transform.position.z);
Instantiate(thePlatform, transform.position, transform.rotation);
}
}
}
平台破坏代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformDestroyer : MonoBehaviour
{
public GameObject platformDestructionPoint;
// Start is called before the first frame update
void Start()
{
platformDestructionPoint = GameObject.Find("PlatformDestructionPoint");
}
// Update is called once per frame
void Update()
{
if(transform.position.x < platformDestructionPoint.transform.position.x)
{
Destroy(gameObject);
}
}
}
【问题讨论】: