首页 > 解决方案 > 人工智能的一个小问题。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 的桨回到它原来的位置。任何人?有什么建议么?我将非常感谢您的任何建议。

标签: unity3dartificial-intelligencepong

解决方案


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

如果您的意思是原始位置作为起始位置,只需将该位置保持为 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.
    }
}

推荐阅读