在上一节中我们提到了相对物体的移动情况,即作为观察者不动时,参照物在移动,参照物的移动似乎也表示着空间内物体距离的变化。
那么观察者就需要清楚一个空间内有多少物体,各个物体的位置坐标在哪里。或者再了解的多一些,就是这些物体都具有什么属性。不过物体对象太多的话,计算也是一个问题。当然物体量不大时,简单的运算就能出来了。我们假设物体量先没有那么大。做一下简单的认识运算。
我们给圆形赋予一个认识能力。让圆形作为机器人输出一些信息。
新建代码AutoKnowObject.cs
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class AutoKnowObject : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//开始发声
Speak();
//告诉刚体类组件情况
AutoKnowRigidbody2D();
//告诉游戏对象情况
AutoKnowObjects();
}
// Update is called once per frame
void Update()
{
}
//主体功能
//1.机器人姓名
String Myname()
{
string name = "RacyenRobot";
return name;
}
//2.机器人讲话
void Speak()
{
String name = Myname();
Debug.Log("My name is " + name+ ". I will gradually know the world!");
Debug.Log("Now time is " + DateTime.Now.ToString());
}
//3.寻找刚体情况
void AutoKnowRigidbody2D()
{
float Rigidbody2DNum=0;
//寻找刚体类型的对象
Rigidbody2D[] bodies = FindObjectsOfType(typeof(Rigidbody2D)) as Rigidbody2D[];
foreach (Rigidbody2D rb in bodies)
{
Debug.Log("Rigidbody2D name is " +rb.name );
Rigidbody2DNum = Rigidbody2DNum + 1;
}
Debug.Log("All Rigidbody2D numbers is " + Rigidbody2DNum+"个");
}
//4.寻找物体
void AutoKnowObjects()
{
float ObjectsNum = 0;
//寻找刚体类型的对象
GameObject[] bodies = FindObjectsOfType(typeof(GameObject)) as GameObject[];
foreach (GameObject go in bodies)
{
Debug.Log("GameObject name is " + go.name);
Debug.Log(go.name+"'s position is "+ AutoKnowObjectsPosion(go));
ObjectsNum = ObjectsNum + 1;
Debug.Log("Foreach numbers is " + ObjectsNum + "个");
}
Debug.Log("All Objects numbers is " + ObjectsNum + "个");
}
//5.寻找物体位置
//物体位置-二维空间
Vector2 AutoKnowObjectsPosion(GameObject go)
{
GameObject gameObject = go;
return gameObject.transform.position;
}
}
我们运行一下程序:
统计一下输出。
My name is RacyenRobot. I will gradually know the world!
Now time is 2022/11/28 1:30:31
Rigidbody2D name is wood_big
Rigidbody2D name is stone_big
Rigidbody2D name is stone_small
Rigidbody2D name is wood_long
Rigidbody2D name is ice
All Rigidbody2D numbers is 5个
GameObject name is leftBorder
leftBorder’s position is (-2.17, 1.52)
Foreach numbers is 1个
GameObject name is rightBorder
rightBorder’s position is (-2.17, 1.52)
Foreach numbers is 2个
GameObject name is wood_big
wood_big’s position is (26.50, -4.50)
Foreach numbers is 3个
GameObject name is Main Camera
Main Camera’s position is (-2.17, 1.52)
Foreach numbers is 4个
GameObject name is stone_big
stone_big’s position is (32.69, -5.42)
Foreach numbers is 5个
GameObject name is ground
ground’s position is (-1.70, -6.70)
Foreach numbers is 6个
GameObject name is upBorder
upBorder’s position is (-2.17, 1.52)
Foreach numbers is 7个
GameObject name is Circle
Circle’s position is (-12.20, -0.20)
Foreach numbers is 8个
GameObject name is background
background’s position is (-2.60, -7.30)
Foreach numbers is 9个
GameObject name is Square
Square’s position is (18.50, 0.00)
Foreach numbers is 10个
GameObject name is slingshot
slingshot’s position is (-34.10, -3.30)
Foreach numbers is 11个
GameObject name is stone_small
stone_small’s position is (32.69, -4.17)
Foreach numbers is 12个
GameObject name is wood_long
wood_long’s position is (26.70, -5.80)
Foreach numbers is 13个
GameObject name is ice
ice’s position is (21.60, -5.40)
Foreach numbers is 14个
All Objects numbers is 14个。
对照我们的环境,验证圆形机器人的输出结果是正确的。
这样圆形机器人初步具备了认知世界的能力。当然他也需要一些学习能力。了解外部世界的变化,物体的移动,这就需要对其能力进行不断补充。