【发布时间】:2020-12-06 17:04:58
【问题描述】:
我在 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 的桨回到它原来的位置。任何人?有什么建议么?我将非常感谢您的任何建议。
【问题讨论】:
标签: unity3d artificial-intelligence pong