【问题标题】:A little problem with AI. Pong Clone in Unity人工智能的一个小问题。 Unity 中的 Pong 克隆
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。现在,当我按照您的建议执行此操作时,AI Paddle 仍然无法回到原位(它在屏幕的垂直边缘冻结)。第二件事是,当我将查找球线替换为启动方法时,出现错误“MissingReferenceException:'GameObject'类型的对象已被破坏,但您仍在尝试访问它。您的脚本应该检查它是否为空或者你不应该销毁对象。AI.FixedUpdate () (at Assets/Scripts/AI.cs:28)"。当其中一名球员得分并实例化新球时,球可能会被破坏。
    • 是的,当你的球被销毁和实例化时,你应该重新找到它。
    • 感谢您所做的一切,您非常有帮助! ;)
    • 如果我能帮到你,请给我的答案打分 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 2017-12-08
    相关资源
    最近更新 更多