unity3d - 人工智能的一个小问题。Unity 中的 Pong 克隆
问题描述
我在 Pong Clone、C# Unity 中制作 AI 时遇到了一点问题。第一个桨由玩家控制,第二个由 AI 控制。我有以下 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);
}
}
}
而且我不知道如何让 AI 的桨回到它原来的位置。任何人?有什么建议么?我将非常感谢您的任何建议。
解决方案
首先,不要在你的固定更新方法中找到你的球,因为它很昂贵而且你不需要。相反,只需在开始时找到它,所以基本上将该行移至您的开始功能。
如果您的意思是原始位置作为起始位置,只需将该位置保持为 vector3 或只是一个浮点数,并且当您希望 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.
}
}
推荐阅读
- c# - WPF 文件夹浏览器对话框,显示外部设备并可以导航到用户给定的文件夹路径
- python - 无法安装pyperclip,Python的多个实例
- java - java - 如何在java中抛出带有特定错误代码的SQLException?
- arduino - 为什么我的 ESP8266 的 EEPROM 不工作?
- azure-data-explorer - 为 Kusto 导出指定多个存储帐户
- django - Django Allauth 和 Salesforce:No_Oauth_State:状态无效
- python - 使用 Python 脚本自动生成评论
- firebase - 如何从图库中设置个人资料图片并保存在 android studio 中的 Firebase
- nuxt.js - 如何拦截 cypress 的服务器端 api 调用
- spring-boot - 从 spring-logback xml 配置中排除文件