c# - 在 Unity 中使用矢量数学生成游戏对象?
问题描述
我正在使用 Unity 开发视频游戏,并且在设计其中一个关卡时遇到了矢量数学问题。我想生成两个平行于屏幕对角线的矩形块: 红色块需要相对于黄色块的位置生成,黄色块需要以某种方式生成,如果我要移动它回到屏幕的对角线,它总是会出现在屏幕对角线上的某个位置,该位置落在屏幕的范围内,例如第二个示例中红线上的某处:我也想这样做确保每当我告诉黄线和红线同时向中心移动时,当它们位于屏幕中心时,它们总是会沿着那条线结束. 我试图确保屏幕的两条对角线以及矩形的四个边都发生这种情况但是由于某种原因,我的数学似乎不起作用。每当条生成时,它们都会在整个地方生成:
这是我编写的当前代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System;
public class BallSpawnerControl3 : MonoBehaviour
{
int index = 1;
public Transform[] spawnPointsLevel3;
int randomSpawnPoint, Interact;
ObjectPooler objectPooler;
public static bool Spawn;
public static int Level3Trans = 1;
private float RandomSpawn;
private float Y_intercept;
float PointBarVectorLength;
float PointBarLength;
Quaternion rot;
private Vector2 Ylength;
private Vector2 Xlength;
Vector2 PointBarVectorConv;
Quaternion PointBarVerticalRot;
Quaternion PointBarDiagonalLeftRot;
Quaternion PointBarDiagonalRightRot;
Quaternion RedBarVerticalRot;
Quaternion RedBarDiagonalLeftRot;
Quaternion RedBarDiagonalRightRot;
Quaternion QuaResult;
public void Start()
{
objectPooler = ObjectPooler.Instance;
GameObject PointBar = (GameObject) Resources.Load("PointBar Horizontal");
GameObject PointBarVertical = (GameObject) Resources.Load("PointBar Vertical");
GameObject PointBarDiagonalLeft = (GameObject)Resources.Load("PointBar Diagonal Left");
GameObject PointBarDiagonalRight = (GameObject)Resources.Load("PointBar Diagonal Right");
GameObject RedBarVertical = (GameObject)Resources.Load("Red Bar Vertical");
GameObject RedBarDiagonalLeft = (GameObject)Resources.Load("Red Bar Diagonal Left");
GameObject RedBarDiagonalRight = (GameObject)Resources.Load("Red Bar Diagonal Right");
PointBarVerticalRot = PointBarVertical.GetComponent<Transform>().rotation;
PointBarDiagonalLeftRot = PointBarDiagonalLeft.GetComponent<Transform>().rotation;
PointBarDiagonalRightRot = PointBarDiagonalRight.GetComponent<Transform>().rotation;
RedBarVerticalRot = RedBarVertical.GetComponent<Transform>().rotation;
RedBarDiagonalLeftRot = RedBarDiagonalLeft.GetComponent<Transform>().rotation;
RedBarDiagonalRightRot = RedBarDiagonalRight.GetComponent<Transform>().rotation;
PointBarVectorLength = PointBar.GetComponent<SpriteRenderer>().bounds.size.x;
PointBarVectorConv = Camera.main.WorldToViewportPoint(new Vector2(PointBarVectorLength, 0f));
rot = new Quaternion(0, 180, 0,0);
Spawn = true;
InvokeRepeating("SpawnAInteract", 0f, 1f);
}
void SpawnAInteract()
{
// Scene scene = SceneManager.GetActiveScene();
if (Spawn && ScoreScript.scoreValue > 11 && SceneManager.GetActiveScene().name == "Level 3")
{
if (ScoreScript.scoreValue > 4 && Level3Trans == 1)
{
StartCoroutine(LevelTransition3());
}
else
{
randomSpawnPoint = UnityEngine.Random.Range(0, spawnPointsLevel3.Length);
Interact = 1;
if (randomSpawnPoint == 0 || randomSpawnPoint == 4)
{
RandomSpawn = UnityEngine.Random.Range(-0.5f, 0.5f - PointBarVectorConv.x);
if (index % 5 != 0)
{
objectPooler.SpawnFromPool("PointBar Horizontal", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(RandomSpawn, 0, 0)), Quaternion.identity);
Debug.Log("PointBar Horizontal");
}
QuaResult = transform.rotation * rot;
objectPooler.SpawnFromPool("Red Bar Horizontal", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(RandomSpawn + PointBarVectorLength, 0, 0)), Quaternion.identity);
objectPooler.SpawnFromPool("Red Bar Horizontal", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(RandomSpawn, 0, 0)), QuaResult);
Debug.Log("PointBar Diagonal Left");
}
else if (randomSpawnPoint == 1 || randomSpawnPoint == 5)
{
if (randomSpawnPoint == 1)
{
RandomSpawn = UnityEngine.Random.Range(0.7f, 1.8f);
Y_intercept = 3.96f;
}
if (randomSpawnPoint == 5)
{
RandomSpawn = UnityEngine.Random.Range(-0.8f, 0.3f);
Y_intercept = -1.085f;
}
int Gradient = -1;
float PointBarYPosition = DiagonalRight(RandomSpawn, Y_intercept);
Vector2 PointBarPosition = Camera.main.ViewportToWorldPoint(new Vector3(RandomSpawn, PointBarYPosition, 0));
if (index % 5 != 0)
{
objectPooler.SpawnFromPool("PointBar Diagonal Left", PointBarPosition, PointBarDiagonalLeftRot);
Debug.Log("PointBar Diagonal Left");
}
QuaResult = RedBarDiagonalLeftRot * rot;
objectPooler.SpawnFromPool("Red Bar Diagonal Left", Camera.main.ViewportToWorldPoint(new Vector3(RedBarDiagonalRightX(RandomSpawn, PointBarVectorLength, Gradient), RedBarDiagonalRightY(PointBarVectorLength, PointBarYPosition, Gradient), 0)), RedBarDiagonalLeftRot);//Adapt this and the next line in order to spawn to red bars attached to the yellow one
objectPooler.SpawnFromPool("Red Bar Diagonal Left", PointBarPosition, QuaResult);
Debug.Log("Red Bar Diagonal Left");
}
else if (randomSpawnPoint == 2 || randomSpawnPoint == 6) // |□|
{
RandomSpawn = UnityEngine.Random.Range(-0.5f, 0.5f - PointBarVectorConv.x);
if (index % 5 != 0)
{
objectPooler.SpawnFromPool("PointBar Vertical", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(0, RandomSpawn, 0)), PointBarVerticalRot);
Debug.Log("Pointbar Vertical");
}
QuaResult = RedBarVerticalRot * rot;
objectPooler.SpawnFromPool("Red Bar Vertical", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(0, RandomSpawn + PointBarVectorLength, 0)), RedBarVerticalRot);
objectPooler.SpawnFromPool("Red Bar Vertical", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(0, RandomSpawn, 0)), QuaResult);
Debug.Log("Red Bar Vertical");
}
else if (randomSpawnPoint == 3 || randomSpawnPoint == 7) // /□/
{
if (randomSpawnPoint == 3)
{
RandomSpawn = UnityEngine.Random.Range(0.7f, 1.8f);
Y_intercept = -2.96f;
}
if (randomSpawnPoint == 7)
{
RandomSpawn = UnityEngine.Random.Range(-0.8f, 0.3f);
Y_intercept = 2.085f;
}
int Gradient = 1;
float PointBarYPosition = DiagonalLeft(RandomSpawn, Y_intercept);
Vector2 PointBarPosition = Camera.main.ViewportToWorldPoint(new Vector3(RandomSpawn, PointBarYPosition, 0));// main might have to be changed too the camera active in Level 3
if (index % 5 != 0)
{
objectPooler.SpawnFromPool("PointBar Diagonal Right", PointBarPosition, PointBarDiagonalRightRot);
Debug.Log("PointBar Diagonal Right");
}
QuaResult = RedBarDiagonalRightRot * rot;
objectPooler.SpawnFromPool("Red Bar Diagonal Right", Camera.main.ViewportToWorldPoint(new Vector3(RedBarDiagonalRightX(RandomSpawn, PointBarVectorLength, Gradient), RedBarDiagonalRightY(PointBarVectorLength, PointBarYPosition, Gradient), 0)), RedBarDiagonalRightRot);// Adapt this and the next line in order to spawn to red bars attached to the yellow one
objectPooler.SpawnFromPool("Red Bar Diagonal Right", PointBarPosition, QuaResult);
Debug.Log("Red Bar Diagonal Right");
}
index++;
}
}
}
private float DiagonalRight(float RandomSpawn, float Y_intercept) // Y = -1X + 14.09 ==> Formula
{
float Yposition = -RandomSpawn + Y_intercept;
return Yposition;
}
private float RedBarDiagonalRightX(float RandomSpawn, float PointBarVectorLength, int Gradient) // Y = -1X + 14.09 ==> Formula
{
float RedXPosition = RandomSpawn + PointBarVectorLength / (float)(Math.Sqrt(1 + Math.Pow(Gradient, 2)));
return RedXPosition;
}
private float RedBarDiagonalRightY(float PointBarVectorLength, float PointBarYPosition, int Gradient) // Y = -1X + 14.09 ==> Formula
{
float RedYPosition = PointBarYPosition + (Gradient * PointBarVectorLength) / (float)(Math.Sqrt(1 + Math.Pow(Gradient, 2)));
return RedYPosition;
}
private float DiagonalLeft(float RandomSpawn, float Y_intercept) // Y = -1X + 14.09 ==> Formula
{
float Yposition = RandomSpawn - Y_intercept;
return Yposition;
}
private IEnumerator LevelTransition3()
{
yield return new WaitForSeconds(1f);
Level3Trans++;
Debug.Log("Level Trans :" + Level3Trans);
}
}
我想指出
1)randomSpawnPoint == Spawnpoint (1+ randomSpawnPoint) 用于此处显示的图片中的位置:
- 我通过将屏幕大小与场景中对象的大小进行比较,手动计算了大部分浮点值。
我现在的问题是对于任何有 Unity 经验的人,如果没有一种简单的方法来做我想要实现的事情,而我似乎并没有掌握。因为考虑到多年来创建的所有游戏,我似乎正在为一些我认为应该相对容易做的事情进行大量计算。或者,如果我的计算明显错误,如果有人可以帮助我朝着正确的方向前进,我会很感激分配?
解决方案
推荐阅读
- c# - 按降序排序datagridviewcolumn C#
- c# - 当我尝试在 C# http Client 中以 JSON 格式获取大量数据时出现错误
- java - Lambda 表达式如何在 Java 中推断类型?
- html - 无法执行 jquery 函数
- python - 如何通过键比较两个数据帧并创建一个新数据帧,但只保留不在第一个数据帧中的键?
- sql - Postgres:在字段中查找具有一个特定值的记录
- regex - 正则表达式替换连字符?
- javascript - 保存应用程序的 JSON 文件内部目录而不要求保存文件
- javascript - 广播 python-socketio 时不支持回调
- amazon-web-services - awscli 的 Dockerfile