人工智能的一个小问题.团结中的克隆人



我在Pong Clone、C#Unity中制作AI时遇到了一个小问题。第一个球拍由玩家控制,第二个由人工智能控制。我有下面的人工智能脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI : MonoBehaviour
{
private Rigidbody2D AIrig;
private GameObject ball;
public float speed = 100;
public float LerpSpeed = 1f;
// Start is called before the first frame update
void Start()
{
AIrig = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void FixedUpdate()
{
ball = GameObject.Find("Ball(Clone)");
if (ball.transform.position.y > transform.position.y)
{
if (ball.transform.position.y < 0)
{

AIrig.velocity = Vector2.Lerp(AIrig.velocity, Vector2.down * speed, LerpSpeed * Time.deltaTime);
}
else
{

AIrig.velocity = Vector2.Lerp(AIrig.velocity, Vector2.up * speed, LerpSpeed * Time.deltaTime);
}
}

if (ball.transform.position.y < transform.position.y && ball.transform.position.x > 1f)
{
if (ball.transform.position.y > 0)
{

AIrig.velocity = Vector2.Lerp(AIrig.velocity, Vector2.up * speed, LerpSpeed * Time.deltaTime);
}
else
{

AIrig.velocity = Vector2.Lerp(AIrig.velocity, Vector2.down * speed, LerpSpeed * Time.deltaTime);
}

}



}

我不知道如何让人工智能的桨回到原来的位置。任何人有什么建议吗?我将非常感谢你的任何建议。

首先,不要在固定日期方法中找到球,因为它很昂贵,而且你不需要。相反,只需在开始时找到它,所以基本上将该行移动到您的开始函数。

如果你的意思是原始位置作为起始位置,只需将该位置保持为向量3或只是一个浮点,当你想让你的AI移动它的原始位置时,只需使用你的相同逻辑或任何其他方式将它移动到那个位置。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI : MonoBehaviour
{
private Vector3 _originalPosition;// original position for the AI
private Rigidbody2D AIrig;
private GameObject ball;
public float speed = 100;
public float LerpSpeed = 1f;
// Start is called before the first frame update
void Start()
{
ball = GameObject.Find("Ball(Clone)"); // finding the ball only 1 time
AIrig = GetComponent<Rigidbody2D>();
_originalPosition = AIrig.transform.position; // getting AI's original position
}

// Update is called once per frame
void FixedUpdate()
{

if (ball.transform.position.y > transform.position.y)
{
if (ball.transform.position.y < 0)
{

AIrig.velocity = Vector2.Lerp(AIrig.velocity, Vector2.down * speed, LerpSpeed * Time.deltaTime);
}
else
{

AIrig.velocity = Vector2.Lerp(AIrig.velocity, Vector2.up * speed, LerpSpeed * Time.deltaTime);
}
}

if (ball.transform.position.y < transform.position.y && ball.transform.position.x > 1f)
{
if (ball.transform.position.y > 0)
{

AIrig.velocity = Vector2.Lerp(AIrig.velocity, Vector2.up * speed, LerpSpeed * Time.deltaTime);
}
else
{

AIrig.velocity = Vector2.Lerp(AIrig.velocity, Vector2.down * speed, LerpSpeed * Time.deltaTime);
}

}
if(canGoToOriginalPosition)// the moment when you want it to go it's original position
{
//here you can just do the same logic and lerp it by comparing it's position or any other way.
}
}

相关内容

  • 没有找到相关文章

最新更新