平台位置随机生成 - 未在可能范围内生成
Platform Positions Randomly Generated - Not Generating within possible range
基本上,我遇到的问题是平台生成垂直,但它们偏向右上角(见图)。因此玩家无法跳到平台,除非我提高他们的速度和跳跃值。我想让平台可以在随机范围内生成,也可以跳转。
平台生成代码:
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);
}
}
}
这是我所经历的图像。
看起来像
transform.position = new Vector3(transform.position.y + platformWidth + distanceBetween, transform.position.x, transform.position.z);
你翻转了 X 和 Y,它应该是
transform.position = new Vector3(transform.position.x + platformWidth + distanceBetween, transform.position.y, transform.position.z);
顺便说一句,你也可以简单地写成
transform.position += new Vector2(platformWidth + distanceBetween, 0);
或者如果您实际上更希望它们也是垂直的(如楼梯),您可能更愿意指的是例如
transform.position += new Vector2(platformWidth, distanceBetween);
基本上,我遇到的问题是平台生成垂直,但它们偏向右上角(见图)。因此玩家无法跳到平台,除非我提高他们的速度和跳跃值。我想让平台可以在随机范围内生成,也可以跳转。
平台生成代码:
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);
}
}
}
这是我所经历的图像。
看起来像
transform.position = new Vector3(transform.position.y + platformWidth + distanceBetween, transform.position.x, transform.position.z);
你翻转了 X 和 Y,它应该是
transform.position = new Vector3(transform.position.x + platformWidth + distanceBetween, transform.position.y, transform.position.z);
顺便说一句,你也可以简单地写成
transform.position += new Vector2(platformWidth + distanceBetween, 0);
或者如果您实际上更希望它们也是垂直的(如楼梯),您可能更愿意指的是例如
transform.position += new Vector2(platformWidth, distanceBetween);