问题描述
这是我在这里的第一篇文章。我在Unity中进行乒乓球游戏,在查找实例化的gameobject球时遇到了问题,我想让它具有对AI控制第二个Paddle进行编程的位置。如下所示:
GameController.cs:
public class GameController : MonoBehavIoUr
{
public GameObject ballPrefab;
public Text score1Text;
public Text score2Text;
public float scoreCoordinates = 3.4f;
private Ball2 currentBall;
private int score1 = 0;
private int score2 = 0;
// Start is called before the first frame update
void Start()
{
SpawnBall();
}
void SpawnBall()
{
GameObject ballInstance = Instantiate(ballPrefab,transform);
currentBall = ballInstance.GetComponent<Ball2>();
currentBall.transform.position = Vector3.zero;
score1Text.text = score1.ToString();
score2Text.text = score2.ToString();
}
// Update is called once per frame
void Update()
{
if (currentBall != null)
{
if (currentBall.transform.position.x > scoreCoordinates)
{
score1++;
Destroy(currentBall.gameObject);
SpawnBall();
}
if (currentBall.transform.position.x < -scoreCoordinates)
{
score2++;
Destroy(currentBall.gameObject);
SpawnBall();
}
}
}
}
AI.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI : MonoBehavIoUr
{
private Rigidbody2D AIrig;
private Ball2 ball;
public float speed = 1f;
// Start is called before the first frame update
void Start()
{
ball = GetComponent<Ball2>();
AIrig = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
ball.transform.position = Vector3.zero;
if (ball.transform.position.y > AIrig.transform.position.y)
{
AIrig.veLocity = new Vector2(0,speed);
}
else if (ball.transform.position.y > AIrig.transform.position.y)
{
AIrig.veLocity = new Vector2(0,-speed);
}
}
}
Ball2.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball2 : MonoBehavIoUr
{
public float minXspeed = 0.8f;
public float maxXspeed = 1.2f;
public float minYspeed = 0.8f;
public float maxYspeed = 1.2f;
private Rigidbody2D ballRigidbody;
// Start is called before the first frame update
void Start()
{
ballRigidbody = GetComponent<Rigidbody2D>();
ballRigidbody.veLocity = new Vector2(Random.Range(minXspeed,maxXspeed) * (Random.value > 0.5f ? -1 : 1),Random.Range(minYspeed,maxYspeed) * (Random.value > 0.5f ? -1 : 1));
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D otherCollider)
{
if (otherCollider.tag == "Limit")
{
if (otherCollider.transform.position.y > transform.position.y && ballRigidbody.veLocity.y > 0)
{
ballRigidbody.veLocity = new Vector2(ballRigidbody.veLocity.x,-ballRigidbody.veLocity.y);
}
if (otherCollider.transform.position.y < transform.position.y && ballRigidbody.veLocity.y < 0)
{
ballRigidbody.veLocity = new Vector2(ballRigidbody.veLocity.x,-ballRigidbody.veLocity.y);
}
}
else if (otherCollider.tag == "Paddle")
{
if (otherCollider.transform.position.x < transform.position.x && ballRigidbody.veLocity.x < 0)
{
ballRigidbody.veLocity = new Vector2(-ballRigidbody.veLocity.x,ballRigidbody.veLocity.y);
}
if (otherCollider.transform.position.x > transform.position.x && ballRigidbody.veLocity.x > 0)
{
ballRigidbody.veLocity = new Vector2(-ballRigidbody.veLocity.x,ballRigidbody.veLocity.y);
}
}
}
}
我无法引用实例化的对象(球)。如果有人可以帮助我,我将不胜感激。
解决方法
好的,我明白了,这是解决方法:
我已经修改了AI.cs,现在看起来像这样:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI : MonoBehaviour
{
private Rigidbody2D AIrig;
public GameObject ball;
public float speed = 1f;
// Start is called before the first frame update
void Start()
{
AIrig = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
ball = GameObject.Find("Ball(Clone)");
if (ball.transform.position.y > AIrig.transform.position.y)
{
AIrig.velocity = new Vector2(0,speed);
}
else if (ball.transform.position.y < AIrig.transform.position.y)
{
AIrig.velocity = new Vector2(0,-speed);
}
}
}
,
我不清楚这些脚本的附加位置。
因此,我不知道您的getComponent
是否可以正常工作。
GetComponent
将起作用
否则,您可以考虑使用诸如FindObjectOfType()
,FindGameObjectsWithTag(string tag)
,GameObject.Find("object name")
之类的选项...
但是我认为最好的方法是,如果您要一个对象引用,然后将实例化的球存储到一个公共变量中,并通过它进行访问。
并且,如果要实例化多个对象,则将它们存储在GameController
的公共列表中。然后在启动功能中获取游戏控制器的引用,您将可以访问这些公共变量。